io.ErrShortBuffer
GoERRORNotableIO

short buffer

Quick Answer

Increase the buffer size or use io.ReadAll to let the runtime manage allocation.

What this means

Returned when the destination buffer is too small to hold the data being read. Resize the buffer or use io.ReadAll.

Why it happens
  1. 1Read buffer allocated with fewer bytes than the incoming message
  2. 2Fixed-size buffer used for variable-length protocol messages

Fix

Use io.ReadAll

Use io.ReadAll
data, err := io.ReadAll(r)
if err != nil { log.Fatal(err) }

Why this works

io.ReadAll grows its internal buffer as needed, preventing ErrShortBuffer.

Code examples
Detectgo
n, err := r.Read(buf)
if errors.Is(err, io.ErrShortBuffer) {
    fmt.Println("buffer too small")
}
Resize buffergo
buf := make([]byte, 4096)
n, err := r.Read(buf)
io.ReadAllgo
data, err := io.ReadAll(r)
fmt.Printf("read %d bytes
", len(data))
Sources
Official documentation ↗

Go standard library

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

← All Go errors