


Downloading the internet, one goroutine at a time!
$ go get github.com/cavaliercoder/grab
Grab is a Go package for downloading files from the internet with the followingrad features:
- Monitor download progress concurrently
- Auto-resume incomplete downloads
- Guess filename from content header or URL path
- Safely cancel downloads using context.Context
- Validate downloads using checksums
- Download batches of files concurrently
Requires Go v1.7+
If you are using an older version of Go, or require previous versions of theGrab API, you can import older version of this package, thanks to gpkg.in.Please see all GitHub tags for available versions.
$ go get gopkg.in/cavaliercoder/grab.v1
The following example downloads a PDF copy of the free eBook, "An Introductionto Programming in Go" and periodically prints the download progress until it iscomplete.
The second time you run the example, it will auto-resume the previous downloadand exit sooner.
package mainimport ("fmt""os""time""github.com/cavaliercoder/grab")funcmain() {// create clientclient:=grab.NewClient()req,_:=grab.NewRequest(".","http://www.golang-book.com/public/pdf/gobook.pdf")// start downloadfmt.Printf("Downloading %v...\n",req.URL())resp:=client.Do(req)fmt.Printf(" %v\n",resp.HTTPResponse.Status)// start UI loopt:=time.NewTicker(500*time.Millisecond)defert.Stop()Loop:for {select {case<-t.C:fmt.Printf(" transferred %v / %v bytes (%.2f%%)\n",resp.BytesComplete(),resp.Size,100*resp.Progress())case<-resp.Done:// download is completebreak Loop}}// check for errorsiferr:=resp.Err();err!=nil {fmt.Fprintf(os.Stderr,"Download failed: %v\n",err)os.Exit(1)}fmt.Printf("Download saved to ./%v\n",resp.Filename)// Output:// Downloading http://www.golang-book.com/public/pdf/gobook.pdf...// 200 OK// transferred 42970 / 2893557 bytes (1.49%)// transferred 1207474 / 2893557 bytes (41.73%)// transferred 2758210 / 2893557 bytes (95.32%)// Download saved to ./gobook.pdf}