The io package provides fundamental interfaces and helper functions for generic I/O operations, allowing you to work with streams of data without needing to know the underlying source or destination. You primarily use it to define contracts like Reader and Writer, or to utilize utility functions like Copy and ReadAll for efficient data movement.
Here is a practical example using io.Copy to transfer data from a file to a network connection. This function handles buffering and chunking automatically, making it more efficient than manually reading and writing in a loop:
package main
import (
"fmt"
"io"
"os"
)
func main() {
// Open a file for reading
src, err := os.Open("input.txt")
if err != nil {
panic(err)
}
defer src.Close()
// Open a file for writing
dst, err := os.Create("output.txt")
if err != nil {
panic(err)
}
defer dst.Close()
// Copy data from src to dst efficiently
// Returns the number of bytes copied
n, err := io.Copy(dst, src)
if err != nil {
panic(err)
}
fmt.Printf("Copied %d bytes\n", n)
}
You can also use io.ReadAll when you need to load an entire stream into memory as a byte slice. This is common when reading small configuration files or request bodies where the size is known to be manageable:
package main
import (
"fmt"
"io"
"os"
)
func main() {
data, err := io.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
// data is now a []byte containing the entire input
fmt.Printf("Read %d bytes: %s\n", len(data), string(data))
}
When implementing your own types, you should implement the io.Reader interface (a single Read(p []byte) (n int, err error) method) or io.Writer interface (a single Write(p []byte) (n int, err error) method). This allows your custom types to be used seamlessly with standard library functions like io.Copy, io.TeeReader, or io.MultiWriter. Remember that Read returns io.EOF when the stream ends, which is distinct from an actual error; io.Copy handles this distinction automatically, but if you write your own loops, you must check for io.EOF explicitly to avoid treating end-of-file as a failure.