io.ErrUnexpectedEOF
GoERRORNotableIO

unexpected EOF

Quick Answer

Handle io.ErrUnexpectedEOF by retrying the request or reporting the upstream source as truncated.

What this means

Returned when a stream ends before the expected number of bytes have been read, usually indicating truncation or a broken connection.

Why it happens
  1. 1Network connection dropped mid-transfer
  2. 2File or buffer truncated before all expected bytes were written

Fix

Detect and retry

Detect and retry
if errors.Is(err, io.ErrUnexpectedEOF) {
    return fmt.Errorf("truncated: %w", err)
}

Why this works

Wrapping preserves the sentinel for callers while adding context.

Code examples
Detectgo
_, err := io.ReadFull(r, buf)
if errors.Is(err, io.ErrUnexpectedEOF) {
    fmt.Println("truncated")
}
io.ReadFull usagego
n, err := io.ReadFull(r, buf)
// returns ErrUnexpectedEOF if n < len(buf)
Distinguish from EOFgo
if err == io.EOF {
    fmt.Println("clean end")
} else if errors.Is(err, io.ErrUnexpectedEOF) {
    fmt.Println("truncated")
}
Same error in other languages
Sources
Official documentation ↗

Go standard library

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Go errors