I/O readers and writers
Design streaming APIs around `io.Reader` and `io.Writer` when data should flow incrementally instead of materializing eagerly.
Canonical guidance
- accept
io.Readerorio.Writerwhen data should stream - keep ownership of closing resources explicit
- use standard adapters before inventing custom interfaces
Use when
- large payloads
- network or file streaming
- pipeline-style transformations
Avoid
- forcing everything through
[]byte - closing readers you did not open unless the contract says so
- custom copy loops without a reason
Preferred pattern
func CopyJSON(dst io.Writer, src io.Reader) error {
_, err := io.Copy(dst, src)
return err
}
Anti-pattern
- reading entire streams into memory when incremental processing would do
Explanation: This anti-pattern is tempting because byte slices are easy to inspect, but it turns streaming problems into memory problems.
Why
- Reader and Writer interfaces are Go’s standard streaming composition points
Related pages
Sources
- io package - Go Team
- Effective Go - Go Team