1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
package main
import (
"fmt"
"io"
"net/http"
"os"
"github.com/schollz/progressbar/v3"
)
func main() {
req, _ := http.NewRequest("GET", "https://dl.google.com/go/go1.14.2.src.tar.gz", nil)
resp, _ := http.DefaultClient.Do(req)
defer check(resp.Body.Close)
f, _ := os.OpenFile("go1.14.2.src.tar.gz", os.O_CREATE|os.O_WRONLY, 0644)
defer f.Close()
bar := progressbar.DefaultBytes(
resp.ContentLength,
"downloading",
)
io.Copy(io.MultiWriter(f, bar), resp.Body)
}
// check checks the returned error of a function.
func check(f func() error) {
if err := f(); err != nil {
fmt.Fprintf(os.Stderr, "received error: %v\n", err)
}
}
|