Downloading large files in Go
阿新 • • 發佈:2018-12-06
package main import ( "fmt" "github.com/cavaliercoder/grab" "time" ) func download(dst string,urls ...string) { n:=len(urls) re,err:=grab.GetBatch(n,dst,urls...) if err!=nil { fmt.Print(err) return } t:=time.NewTicker(time.Millisecond*10) complete:=0 progress:=0 responses:=make([]*grab.Response,0) for complete<n { select { case r:=<-re: responses=append(responses,r) case <-t.C: progress=0 for k,v:=range responses{ if v!=nil { if v.IsComplete() { if v.Err()==nil { fmt.Printf("%s %s/%s %d%%\n",v.Filename,ShowBytes(float64(v.BytesComplete())),ShowBytes(float64(v.Size)),int(v.Progress()*100)) } else { fmt.Printf("%s:%s\n",v.Filename,v.Err()) } responses[k]=nil complete++ } else { fmt.Printf("%s %s/%s %d%%\n",v.Filename,ShowBytes(float64(v.BytesComplete())),ShowBytes(float64(v.Size)),int(v.Progress()*100)) progress++ } } } if progress>0 { fmt.Printf("%d downloading\n",progress) } } } t.Stop() } func ShowBytes(bytesnum float64) string { if bytesnum<1024 { return fmt.Sprintf("%.2fB",bytesnum) } else if bytesnum<1024*1024 { return fmt.Sprintf("%.2fKB",bytesnum/1024.0) } else if bytesnum<1024*1024*1024 { return fmt.Sprintf("%.2fMB",bytesnum/1024.0/1024.0) } else { return fmt.Sprintf("%.2fGB",bytesnum/1024.0/1024.0/1024.0) } } func main() { urls:=[]string{ "https://rpic.douyucdn.cn/live-cover/appCovers/2018/08/31/3279944_20180831104533_small.jpg", "https://rpic.douyucdn.cn/live-cover/roomCover/2018/11/06/0a699f47dc4fc55deaa82402cc0876ea_big.png", "https://rpic.douyucdn.cn/live-cover/appCovers/2018/10/19/5230163_20181019161115_small.jpg", } download("download",urls...) }