Go语言实现Windows命令行下载文件.

来源:转载

package mainimport ( "fmt" "io" "net/http" "os" "strings" "sync")var go_lock sync.WaitGroupfunc 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()}



分享给朋友:
您可能感兴趣的文章:
随机阅读: