mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-09-11 22:31:36 +00:00
good good study, day day up!
This commit is contained in:
34
vendor/gopl.io/ch1/fetch/main.go
generated
vendored
Normal file
34
vendor/gopl.io/ch1/fetch/main.go
generated
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
// See page 16.
|
||||
//!+
|
||||
|
||||
// Fetch prints the content found at each specified URL.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
for _, url := range os.Args[1:] {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "fetch: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "fetch: reading %s: %v\n", url, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("%s", b)
|
||||
}
|
||||
}
|
||||
|
||||
//!-
|
Reference in New Issue
Block a user