Io Copy
Fast recall
Quick revision
Core answer
Whether you understand Io Copy beyond memorized syntax.
Giving a definition without explaining a practical trade-off.
Connect the concept to a small production example and state its complexity or failure mode.
Interview-ready guide
Interview answer
The signature and behavior:
n, err := io.Copy(dst, src)
// n = bytes copied
// err = read or write error (nil on clean EOF)The pattern — file copy in three lines:
src, _ := os.Open("input.txt")
defer src.Close()
dst, _ := os.Create("output.txt")
defer dst.Close()
n, _ := io.Copy(dst, src)
// n bytes copied — the entire file, chunk by chunk internallyWhat it does: reads 32KB chunks from src, writes each to dst, repeats until EOF. You get the whole-stream transfer with no loop, no buffer management, no partial-read complexity.
The generalization: io.Copy works on ANY io.Reader → io.Writer pair. Files, network connections, HTTP bodies, buffers, pipes. The pattern: proxy an HTTP response to a file — io.Copy(file, resp.Body) — the download helper. Or stream a request body to a response — the proxy.
The optimization it exploits: if src implements io.ReaderTo or dst implements io.WriterFrom, io.Copy delegates (a sendfile-style direct path — kernel-level copy, no userspace buffer). The interfaces are invisible — io.Copy just uses them when present. That's why the right answer to 'how do I copy a stream efficiently?' is io.Copy, not a manual loop.
Io Copy
Direct Answer
io.Copy(dst, src) reads from src and writes to dst until EOF — the whole stream in one call. It uses an internal buffer; returns bytes copied. The universal stream-to-stream primitive.
Deep Dive & Architecture
The interface-delegation mechanism: io.Copy checks src.(io.ReaderTo) — if dst implements WriteTo(src), src.WriteTo(dst) runs (the source drives — often with kernel assistance like sendfile for files-to-sockets). And dst.(io.WriterFrom) — the dest drives via ReadFrom(src). Both bypass the generic 32KB buffer entirely — the zero-copy paths when the types know better.
The CopyBuffer variant: io.CopyBuffer(dst, src, buf) — you supply the buffer (a control Copy doesn't expose). CopyN limits: io.CopyN(dst, src, n) — exactly n bytes (EOF is an error if fewer arrived — the length-constrained variant).
The Composition patterns: TeeReader (read to TWO destinations — io.Copy(tee, ...) — the tee pattern for logging while processing); MultiWriter (write to multiple — the fan-out); the io.Pipe (in-process streams — producer/consumer without channels). These are the building blocks for stream-processing pipelines.
Common Mistakes & Pitfalls
Giving a definition without explaining a practical trade-off.
Check your understanding
Answer these without looking back at the article.
- When would you use Io Copy in a real project?
- How would you compare Io Copy with an alternative?
- What is a common mistake when using Io Copy?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.