io.ErrShortWrite
GoERRORNotableIO
short write
Quick Answer
Use io.WriteString or loop until all bytes are written, checking n against len(data).
What this means
Returned when a Write call writes fewer bytes than requested without returning an explicit error. Indicates a partial write condition.
Why it happens
- 1Underlying writer accepted fewer bytes than provided in a single call
- 2Network buffer full causing partial write without an OS error
Fix
Use io.WriteString or loop
Use io.WriteString or loop
_, err := io.WriteString(w, s)
if err != nil { log.Fatal(err) }Why this works
io.WriteString retries internally until all bytes are accepted or an error occurs.
Code examples
Detectgo
n, err := w.Write(data)
if n < len(data) && err == nil {
err = io.ErrShortWrite
}Write loopgo
for len(data) > 0 {
n, err := w.Write(data)
data = data[n:]
if err != nil { break }
}bufio.Writer flushgo
bw := bufio.NewWriter(w)
bw.WriteString("hello")
bw.Flush()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