io.Copy
The Go function that streams all the bytes from an io.Reader to an io.Writer and returns how many it copied.
In more detail
It moves data in small chunks, so memory use stays flat however large the stream is. Use it to save a download to a file or pass a body straight through.
Example
n, err := io.Copy(&target, source)Related terms
- io.Reader — The Go interface for anything you can read bytes from, such as a file, a network connection or an HTTP response body.
- io.Writer — The Go interface for anything you can write bytes to, such as a file, standard output, a buffer or an HTTP response.
- io.ReadAll — The Go function that reads an io.Reader until the end and returns everything as one slice of bytes.
Taught in
- Streams and the io Package — Concurrent Systems Programming with Go