io.Reader.Read, io.EOF
type Reader interface {
Read(p []byte) (n int, err error)
}
var EOF = errors.New("EOF")Read into a buffer. A successful read may be shorter than len(p). A later empty read can return EOF.
| Parameter | Description |
|---|---|
p | The buffer to fill. n is the number of bytes placed in p, and only p[:n] is valid. |
Returns: The number of bytes read and an error. 0 and EOF means there is no more input.
Errors
io.EOF— No more input is available. Compare this sentinel with ==, or with errors.Is. Do not wrap EOF when implementing Read.
- This strings.Reader example returns 3, nil and then 0, EOF. Other readers may combine the last bytes and EOF in one call.
package main
import (
"fmt"
"strings"
)
func main() {
r := strings.NewReader("abc")
buf := make([]byte, 8)
n, err := r.Read(buf)
fmt.Printf("%d %v %q\n", n, err, buf[:n])
n, err = r.Read(buf)
fmt.Printf("%d %v\n", n, err)
}Expected output
3 <nil> "abc"
0 EOF