io.EOF
GoINFOCommonIO
EOF
Quick Answer
Compare err == io.EOF directly to detect end-of-stream and stop reading.
What this means
Signals the end of a readable stream; not an error in the traditional sense but a sentinel value used to stop reading loops.
Why it happens
- 1Reader has no more data to return
- 2HTTP response body fully consumed
Fix
Break on EOF in read loop
Break on EOF in read loop
for {
_, err := r.Read(buf)
if err == io.EOF { break }
if err != nil { log.Fatal(err) }
}Why this works
Treating io.EOF as a normal loop terminator prevents false error reporting.
Code examples
Read loopgo
buf := make([]byte, 512)
for {
n, err := r.Read(buf)
if err == io.EOF { break }
_ = n
}bufio.Scanner avoids EOFgo
s := bufio.NewScanner(r)
for s.Scan() { fmt.Println(s.Text()) }io.ReadAllgo
data, err := io.ReadAll(r) // EOF handled internally
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