package main import ( "fmt" "io" "net/http" "os" "strings" "sync" ) var go_lock sync.WaitGroup func main() { if len(os.Args) < 2 { fmt.Println("Param error!") return } for _, v := range os.Args[1:] { go_lock.Add(1) fmt.Printf("Get %s\n", v) go client(v) } go_lock.Wait() } func client(url string) { resp, err := http.Get(url) if err != nil { fmt.Println(err) return } fmt.Printf("Host %s \nrequest status :%d\nfilelength :%d bytes\n", resp.Request.URL.Host, resp.StatusCode, resp.ContentLength) defer func() { resp.Body.Close() go_lock.Done() }() path := resp.Request.URL.Path if !strings.HasSuffix(path, "/") { path = path + "/" } list := strings.Split(path, "/") filename := list[len(list)-2] File, err := os.Create(filename) if err != nil { fmt.Println(err) return } io.Copy(File, resp.Body) File.Close() }
版权声明:本文为博主原创文章,未经博主允许不得转载。