io.Copy vs reading into memory

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:

  1. 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()
  1. 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 :man_shrugging:t5: so i don’t see the difference. :thinking: :thinking:

Both methods are pretty much doing the same thing, they both use a buffer. Please check the following source codes:

ioutil.ReadAll:
https://cs.opensource.google/go/go/+/master:src/io/ioutil/ioutil.go;drc=3d913a926675d8d6fcdc3cfaefd3136dfeba06e1;l=26

io.Copy:
https://cs.opensource.google/go/go/+/refs/tags/go1.17.1:src/io/io.go;drc=dc289d3dcb59f80b9e23c7e8f237628359d21d92;l=381