io.ReadFull

The Go function that reads exactly as many bytes as a buffer holds, returning an error if the stream ends first.

In more detail

Use it when a format requires a fixed-size piece, such as a header of known length. A short read becomes an explicit error (io.ErrUnexpectedEOF) instead of silently partial data.

Example

buf := make([]byte, 4)
_, err := io.ReadFull(r, buf)

Related terms

Taught in

References