🐹 Go (Golang) Intermediate

How do io.Reader and io.Writer work in Go?

Answer

io.Reader and io.Writer are two of Go's most important interfaces. Reader has one method: Read(p []byte) (n int, err error). Writer has: Write(p []byte) (n int, err error). The power lies in composability — any type implementing Reader (files, HTTP response bodies, network connections, buffers, gzip decompressors) can be passed to any function that accepts io.Reader. The standard library's io.Copy, bufio.Scanner, json.NewDecoder, and many others all work with these interfaces. This design lets you build pipelines without knowing the concrete source or destination.