Go IO Package
The io package provides the fundamental building blocks for working with data streams. Almost everything in Go that reads or writes data (files, network connections, buffers) uses the interfaces defined here.
1. Core Interfaces
There are two primary interfaces you will see everywhere:
Reader: Anything that has aRead(p []byte)method. It fills the slicepwith data.Writer: Anything that has aWrite(p []byte)method. It sends data from the slicepto a destination (like a file).
2. Common Utility Functions
io.Copy(dst, src): Read everything fromsrcand write it todstuntil EOF.io.ReadAll(r): Reads everything from a reader into a single[]byte.io.WriteString(w, s): A convenience function to write a string directly to a writer.
3. Combining Streams
io.MultiReader(r1, r2, ...): Combines multiple readers into one big reader that reads them in order.io.MultiWriter(w1, w2, ...): A single writer that duplicates every write to multiple destinations (useful for logging to both terminal and file).io.LimitReader(r, n): Reads at mostnbytes fromr.io.TeeReader(r, w): Returns a reader that writes everything it reads tow(like the Unixteecommand).
Why use io interfaces?
- Flexibility: You can write a function that takes an
io.Reader, and it will work with files, HTTP bodies, ZIP archives, or plain strings without any changes. - Efficiency: By using streams instead of loading everything into memory at once, your program can process gigabytes of data using only a few kilobytes of RAM.
- Standardization: Because the whole Go ecosystem uses these interfaces, libraries usually "just work" together.