Hello, i was wondering what’s the difference between reading a file into memory and using io.Copy to write it to the destination. Here is an example to be more clear:
- Reading file into memory
file, err := os.Open(path)
if err != nil { return }
fileContents, err := ioutil.ReadAll(file)
if err != nil { return }
fi, err := file.Stat()
if err != nil { return }
file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, fi.Name())
if err != nil { return }
part.Write(fileContents)
writer.Close()
- Using io.Copy
file, err := os.Open(path)
if err != nil {
return
}
defer file.Close()
fi, err := file.Stat()
if err != nil {
return
}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, fi.Name())
if err != nil {
return
}
io.Copy(part, file)
I kinda think that even if the copy method writes it directly to the part writer, still that variable is in the memory so i don’t see the difference.