The io package defines the small interfaces that almost all Go I/O is built on: io.Reader for sources of bytes and io.Writer for destinations. Files, network connections, HTTP bodies, buffers and compressors all implement them, so the same code can copy, wrap and transform data from any of them.
This guide shows the core interfaces, the helper functions you will use daily, and how they combine with os, bufio and bytes. Examples use current Go (1.22 and later).
Both interfaces have a single method:
Read fills p with up to len(p) bytes and returns how many it wrote. It may return fewer bytes than requested, and it may return n > 0 together with an error, so always process the n bytes before checking err. At the end of the data it returns io.EOF, which is a normal signal and not a failure.
Write must return a non-nil error if it writes fewer than len(p) bytes.
For small files, os.ReadFile is the simplest option. It opens, reads and closes the file in one call.
For any io.Reader (a network body, for example), use io.ReadAll. It reads until io.EOF and returns the bytes, but it loads everything into memory, so wrap untrusted input in io.LimitReader first.
The old ioutil.ReadFile and ioutil.ReadAll functions are deprecated since Go 1.16. Use os.ReadFile and io.ReadAll.
io.Copy(dst, src) streams from any reader to any writer with a small internal buffer, so memory use stays constant for files of any size.
io.Copy returns nil at io.EOF, so a nil error means the copy completed. Check the error from dst.Close() too when the data must be durable, because buffered writes can fail at close time.
Raw Read calls on a file are small system calls. Wrap the reader in bufio to batch them, and use bufio.Scanner for lines.
A scanner rejects lines longer than 64 KiB by default. Call scanner.Buffer to raise the limit, or use bufio.Reader.ReadString('\n') for arbitrary lines. For output, bufio.NewWriter batches writes; call Flush before you finish.
Because the interfaces are tiny, they compose. These helpers cover most needs:
| Helper | What it does |
|---|---|
io.MultiWriter(w1, w2) | Writes the same bytes to several writers, for example a file and a hash |
io.MultiReader(r1, r2) | Reads one reader after another as a single stream |
io.TeeReader(r, w) | Copies everything read from r into w as a side effect |
io.LimitReader(r, n) | Stops with io.EOF after n bytes |
io.Pipe() | Connects a writer in one goroutine to a reader in another |
io.Discard | A writer that drops all data |
For example, to save a download and compute its checksum in one pass:
For in-memory data, strings.NewReader and bytes.NewReader turn a string or byte slice into a reader, and bytes.Buffer is both a reader and a writer, which makes tests simple.
Any type with the right method satisfies the interface. This writer counts the bytes that pass through it:
Accept io.Reader and io.Writer in your function parameters instead of *os.File. Your code then works with files, sockets and test buffers without changes.
io.EOF means the source has no more data. Treat it as the end of input, not a failure. Compare with errors.Is(err, io.EOF).
os.ReadFile takes a path and manages the file for you. io.ReadAll takes any io.Reader. Both load the entire content into memory.
A reader returns whatever is available, which is normal for network and pipe sources. Use io.ReadFull when you need exactly len(buf) bytes.
It still works, but the package is deprecated. Its functions moved to io and os, so use those in new code.
Build on io.Reader and io.Writer, stream with io.Copy instead of loading everything, wrap raw files in bufio, and compose behaviour with MultiWriter, TeeReader and LimitReader. Handle io.EOF as a normal end of data and prefer os.ReadFile and io.ReadAll over the deprecated ioutil functions.