[Golang] Download File From URL
Download and save files (image, pdf, etc.) from given URL in Go.
import (
"fmt"
"io"
"net/http"
"os"
"path"
)
func download(url string) (err error) {
filename := path.Base(url)
fmt.Println("Downloading ", url, " to ", filename)
resp, err := http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
f, err := os.Create(filename)
if err != nil {
return
}
defer f.Close()
_, err = io.Copy(f, resp.Body)
return
}
Tested on: Ubuntu Linux 18.04, Go 1.10.1.
References:
[1] | [Golang] Download HTML From URL |
[2] | save image from url at DuckDuckGo |
[3] | download file from url - Google Search |