liveBook · Manning

Io.Copy

As explained in Go in Action, Second Edition

io.Copy transfers data from a Reader to a Writer until the input reaches end of file or an error occurs.

io.Copy transfers data from any io.Reader to any io.Writer until the input reaches end of file or an error occurs. It provides a general stream-copying operation without tying the implementation to files or another specific source and destination. section 10.3

The function fits Go’s composable I/O design: callers can connect different sources and sinks through the small io.Reader and io.Writer interfaces. Use io.CopyN instead when the operation must stop after a specific byte count. section 10.3

Files and streaming data

Use io.Copy for copying files or streaming data between sources and sinks, while choosing io.CopyN for copying only part of a stream. section 10.3

Figure 10.1 io.Reader and io.Writer interface composition with multiple package implementations

The interface-composition figure helps map io.Copy’s generic operation across multiple Reader and Writer implementations. section 10.3

How Go in Action, Second Edition covers it

How deep it goes
Defines efficient Reader-to-Writer transfer and its EOF-or-error boundary, but defers implementation internals.
Examples
Uses file copying, streaming between sources and sinks, and partial-stream copying as practical cases.
What it assumes
Assumes familiarity with the io.Reader and io.Writer interfaces and their composable stream model.
What you can do after
Apply it via io.Copy to move data between any compatible Reader and Writer.
How it connects
Connects io.Reader, io.Writer, io.CopyN, and stream composition.

Also explained in

Compare how 2 books explain io.Copy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.