package main import ( "fmt" "io" //"io/ioutil" "net/http" "os" "strings" ) func main() { ch := make(chan bool, len(os.Args[1:])) for _, url := range os.Args[1:] { if !strings.HasPrefix(url, "http://") { url = "http://" + url } go func(url string) { resp, err := http.Get(url) if err != nil { fmt.Println(err) os.Exit(1) } defer resp.Body.Close() write, err := io.Copy(os.Stdout, resp.Body) //b, err := ioutil.ReadAll(resp.Body)//申请一个缓冲区来储存 if err != nil { fmt.Println(err) os.Exit(1) } fmt.Printf("%s", write) ch <- true }(url) } for i := 0; i < len(os.Args[1:]); i++ { fmt.Printf("%s", i) <-ch } }
控制台上go build first.go
first www.baidu.com http://www.csdn.net
有疑问加站长微信联系(非本文作者)