The `io` package in Go
The io
package in Go
-
io.Read()
Reads data from a source (like a file or network connection) into a byte slice.n, err := io.Read(p []byte) -
io.ReadAtLeast()
Reads at least the specified number of bytes from a source.n, err := io.ReadAtLeast(r io.Reader, p []byte, min int) -
io.ReadFull()
Reads exactly the number of bytes specified by the length of the provided byte slice.n, err := io.ReadFull(r io.Reader, p []byte) -
io.Write()
Writes data from a byte slice to a destination.n, err := io.Write(p []byte) -
io.Copy()
Copies data from a source (reader) to a destination (writer) until EOF.n, err := io.Copy(dst io.Writer, src io.Reader) -
io.CopyN()
Copies exactlyn
bytes from the reader to the writer.n, err := io.CopyN(dst io.Writer, src io.Reader, n int64) -
io.TeeReader()
Creates a reader that reads from the underlying reader and writes to the writer.tee := io.TeeReader(r io.Reader, w io.Writer) -
io.MultiReader()
Combines multiple readers into a single reader that reads from each of them sequentially.r := io.MultiReader(r1, r2) -
io.MultiWriter()
Combines multiple writers into a single writer that writes to all of them.w := io.MultiWriter(w1, w2) -
io.Discard
AWriter
that discards all data written to it. Useful for ignoring output.
io.Discard.Write(p []byte)
These functions are widely used for handling input/output operations efficiently in Go, especially when working with streams, files, or network connections.