This post shows how you can download a file in Go from a URL. We use the std lib (standard library) http.Get() [docs] and io.Copy() [docs] functions to help us with this. This function should be efficient as it will stream the data into the file, as opposed to downloading it all into memory, then to file.
The file will be saved in the same directory as your program.
We also show an alternative below if you want to take the filename from the URL.
packagemainimport("fmt""io""net/http""os")funcmain(){fileUrl:="https://gophercoding.com/img/logo-original.png"// Download the file, params:
// 1) name of file to save as
// 2) URL to download FROM
err:=DownloadFile("saveas.png",fileUrl)iferr!=nil{fmt.Println("Error downloading file: ",err)return}fmt.Println("Downloaded: "+fileUrl)}// DownloadFile will download from a given url to a file. It will
// write as it downloads (useful for large files).
funcDownloadFile(filepathstring,urlstring)error{// Get the data
resp,err:=http.Get(url)iferr!=nil{returnerr}deferresp.Body.Close()// Create the file
out,err:=os.Create(filepath)iferr!=nil{returnerr}deferout.Close()// Write the body to file
_,err=io.Copy(out,resp.Body)returnerr}
Alternative Filename
In our example above, you needed to provide it with a file name to save as - but we can replace this, if we want, to use the original filename in the url. We’ve added a line to work out the filename from the url: path.Base(resp.Request.URL.String()).
funcDownloadFile(urlstring)error{// Get the data
resp,err:=http.Get(url)iferr!=nil{returnerr}deferresp.Body.Close()// Create the file
filepath:=path.Base(resp.Request.URL.String())out,err:=os.Create(filepath)iferr!=nil{returnerr}deferout.Close()// Write the body to file
_,err=io.Copy(out,resp.Body)returnerr}
Edd is a PHP and Go developer who enjoys blogging about his experiences, mostly about creating and coding new things he's working on and is a big beliver in open-source and Linux.
Get/Set HTTP Headers in Go Request
–
HTTP headers, we all need ’em 😉
Here’s how you can get and set these headers within your Go requests. These include request coming into your router handlers and requests you are sending out to other systems through net/http. This can be thought of as being the same as reading headers from a request and creating new ones.
First we’ll start with reading them from the request.
Get Status Code from HTTP Request
–
We won’t go into too much detail about HTTP status codes themselves, but in this post we will talk about how to use the status code after making a request, how to check them as a range and how to print them as text. This is often important so we can check if something was successful or failed.
You can always get this data if you have a net/http/Response type (spec).
Convert an io.ReadCloser to String
–
Using Go, we often make HTTP calls these days using net/http, which result in a response of type io.ReadCloser… which are hard to read for the layman (like me). What we really want to the response in the form of a string which we can read. This post will talk about how to convert these ReadCloser into strings.
First we’ll look at the problem, then we have two different solutions.