Go io.ErrUnexpectedEOF
ReadFull reports ErrUnexpectedEOF when a stream ends partway through the requested buffer.
A simple example
io.ErrUnexpectedEOF
- Start with One available byte
- Do this Try to read a complete two-byte field
- Result One byte is missing
Syntax
io.ReadFull(reader, buf)
Run these examples in Go
Save one example at a time as main.go. Use Go 1.21 or later.
go run main.goRead a stock count from a short record
A stock scanner sends each count as a four-byte integer. A transfer stops after two bytes, so the receiver must request the record again before posting the count to inventory.
package main
import (
"bytes"
"errors"
"fmt"
"io"
)
func main() {
scanner := bytes.NewReader([]byte{0, 5})
record := make([]byte, 4)
_, err := io.ReadFull(scanner, record)
if errors.Is(err, io.ErrUnexpectedEOF) {
fmt.Println("Request the complete record")
}
}
Request the complete record
The two-byte transfer falls short of the four bytes required for a stock count.
Decode a complete gate count
A counter sends one four-byte record for the entrance. The device format requires a four-byte integer. Request another transmission if the input ends before all four bytes arrive.
Detect a cut-off meter record
A sensor connection closes after the first two bytes of a four-byte record. The device format requires a four-byte integer. Request another transmission if the input ends before all four bytes arrive.
What to watch for
An entirely empty reader returns io.EOF. A short record that has already started is a different condition.