HowtoGo
Home / Standard Library / The io Package
Standard Library

The io Package

io defines the Reader, Writer, and Closer interfaces every other I/O package builds on, plus a set of functions for copying, combining, and wrapping them without caring what's on the other end.

Examples

Copy reads src to EOF and writes everything to dst, returning the byte count it moved.

src := strings.NewReader("hello, io")
var dst bytes.Buffer

n, err := io.Copy(&dst, src)
fmt.Println(n, err)
fmt.Println(dst.String())
Output
9 
hello, io

Interfaces, Functions & Types

FunctionDescription
EOF
Sentinel error returned by Read when no more input is available.
ErrUnexpectedEOF
Returned when EOF arrives mid-structure, after some but not all of the expected data was read.
ErrShortWrite
Returned when a write accepted fewer bytes than it was given, with no other explanation.
ErrShortBuffer
Returned when a read needs a larger buffer than the one it was given.
ErrClosedPipe
Returned by Pipe operations performed after the pipe has already been closed.
ErrNoProgress
Returned by callers of ReadAtLeast-style loops after many consecutive reads return no data and no error.
SeekStart
Seek whence value: offset is relative to the start of the file.
SeekCurrent
Seek whence value: offset is relative to the current offset.
SeekEnd
Seek whence value: offset is relative to the end.
Discard
A Writer that discards everything written to it, like /dev/null.
Reader1 method
The source half of the package. Anything bytes can be read from.
Writer1 method
The destination half. Anything bytes can be written to.
Closer1 method
Anything holding a resource that has to be released.
Seeker1 method
Anything with a cursor that can be moved.
ReaderAt1 method
Reads from a fixed offset, leaving any cursor where it was.
WriterAt1 method
Writes at a fixed offset, leaving any cursor where it was.
ReaderFrom1 method
A destination that knows how to pull from a source itself.
WriterTo1 method
A source that knows how to push itself to a destination.
ByteReader1 method
Reads one byte at a time, without a slice.
ByteScanner2 methods
A ByteReader that can put the last byte back.
ByteWriter1 method
Writes one byte at a time, without a slice.
RuneReader1 method
Reads one UTF-8 character at a time.
RuneScanner2 methods
A RuneReader that can put the last character back.
StringWriter1 method
Accepts a string directly, skipping the []byte conversion.
ReadWriter2 methods
Reader and Writer.
ReadCloser2 methods
Reader and Closer. The shape of an HTTP request body.
WriteCloser2 methods
Writer and Closer.
ReadWriteCloser3 methods
Reader, Writer and Closer. The shape of a network connection.
ReadSeeker2 methods
Reader and Seeker. What http.ServeContent wants.
WriteSeeker2 methods
Writer and Seeker.
ReadWriteSeeker3 methods
Reader, Writer and Seeker.
ReadSeekCloser3 methods
Reader, Seeker and Closer.
Copy(dst, src) (written int64, err error)
io.Copy(&buf, r) // 9, nil
Copies from src to dst until EOF on src or an error.
CopyBuffer(dst, src, buf) (written int64, err error)
Like Copy, but uses the caller-supplied buffer instead of allocating one.
CopyN(dst, src, n) (written int64, err error)
io.CopyN(&buf, r, 5) // 5, nil
Copies exactly n bytes from src to dst, or returns an error trying.
ReadAll(r Reader) ([]byte, error)
io.ReadAll(r) // []byte("hello"), nil
Reads from r until EOF and returns everything read; EOF itself is not reported as an error.
ReadAtLeast(r, buf, min) (n int, err error)
Reads from r into buf until at least min bytes have arrived.
ReadFull(r Reader, buf []byte) (n int, err error)
Reads from r until buf is completely full.
WriteString(w Writer, s string) (n int, err error)
io.WriteString(w, "hi") // 2, nil
Writes s to w, calling w's own WriteString method directly when w implements StringWriter.
LimitReader(r Reader, n int64) Reader
io.LimitReader(r, 3)
Returns a Reader that reads from r but reports EOF after n bytes.
LimitedReader
The struct LimitReader returns: exported fields R Reader and N int64, the remaining byte budget.
MultiReader(readers ...Reader) Reader
io.MultiReader(r1, r2) // reads r1 fully, then r2
Concatenates readers into one logical Reader, read in sequence.
MultiWriter(writers ...Writer) Writer
io.MultiWriter(w1, w2)
Duplicates every write across all of the given writers.
TeeReader(r Reader, w Writer) Reader
io.TeeReader(r, hasher)
Returns a Reader that also writes to w everything read from r, as it's read.
NopCloser(r Reader) ReadCloser
Wraps r with a no-op Close method, for call sites that require a ReadCloser but need no real cleanup.
OffsetWriter
A WriterAt that adds a fixed base offset to every WriteAt call.
NewOffsetWriter(w WriterAt, off int64) *OffsetWriter
Constructs an OffsetWriter around w, starting at off.
SectionReader
A ReadSeeker and ReaderAt limited to a fixed section of an underlying ReaderAt.
NewSectionReader(r ReaderAt, off, n int64) *SectionReader
io.NewSectionReader(r, 5, 5)
Constructs a SectionReader over n bytes of r starting at off.
(*SectionReader) Read(p []byte) (n int, err error)
Reads within the section, advancing the section's own internal offset.
(*SectionReader) ReadAt(p []byte, off int64) (n int, err error)
Reads at off relative to the section's start, ignoring and not advancing the internal offset.
(*SectionReader) Seek(offset int64, whence int) (int64, error)
Moves the section's internal offset, bounded by the section's own start and length.
(*SectionReader) Size() int64
Returns the section's fixed length, exactly the n given to NewSectionReader.
Pipe() (*PipeReader, *PipeWriter)
Creates an in-memory, synchronous pipe connecting a Reader end and a Writer end.
PipeReader
The read half of a Pipe; each Read blocks until a matching Write supplies data.
PipeWriter
The write half of a Pipe; each Write blocks until a matching Read consumes it.