io.Reader
The Go interface for anything you can read bytes from, such as a file, a network connection or an HTTP response body.
In more detail
It has one method, Read, which fills a byte slice and returns how many bytes it read and an error, with io.EOF meaning the end. Writing functions to accept an io.Reader instead of a file name makes them work with any source and easy to test.
Example
reader := strings.NewReader("0123456789")Related terms
- io.Writer — The Go interface for anything you can write bytes to, such as a file, standard output, a buffer or an HTTP response.
- io package — Go's standard library package of small interfaces and helpers for moving streams of bytes, centred on io.Reader and io.Writer.
- interface — A named set of member signatures that a type promises to implement, with no state of its own.
- io.Copy — The Go function that streams all the bytes from an io.Reader to an io.Writer and returns how many it copied.
Taught in
- Interfaces and Why They Are Implicit — Concurrent Systems Programming with Go