Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way in Go to stream io.ReadCloser to a file?

Tags:

go

Given an io.ReadCloser, from the response of an HTTP request for example, what is the most efficient way both in memory overhead and code readability to stream the response to a File?

like image 669
mbrevoort Avatar asked Aug 12 '15 02:08

mbrevoort


1 Answers

io.Copy is undoubtedly the most efficient in terms of code; you only need to

outFile, err := os.Create(filename)
// handle err
defer outFile.Close()
_, err = io.Copy(outFile, res.Body)
// handle err

it's also likely to be pretty efficient in terms of CPU and memory as well. You can peek at the implementation of io.Copy if you want; assuming that the body doesn't implement WriteTo and the file doesn't implement ReadFrom (a quick glance says that they don't), Copy will copy chunks of up to 32kB at a time. A bigger chunk would probably use a bit less CPU but more memory; the value they picked seems like a good tradeoff.

like image 89
hobbs Avatar answered Oct 12 '22 16:10

hobbs