Stream data with Go readers and writers
8 steps · about 1 h 55 min · Intermediate
- Meet io.Reader in the Go tour (interactive, Go.dev, 10 min)
A runnable exercise showing the Read contract: fill a buffer, return n and io.EOF.
- Learn the io interfaces and helpers (reference, Go standard library, 25 min)
Copy, TeeReader, MultiWriter, LimitReader and Pipe are the building blocks for streaming code.
- Buffer reads and scan lines (reference, Go standard library, 20 min)
bufio.Scanner and bufio.Writer turn many small reads and writes into efficient ones.
- Read files in chunks and with seeks (tutorial, Go by Example, 10 min)
Short annotated program showing os.Open, Read, Seek and bufio together.
- Write files and flush buffered output (tutorial, Go by Example, 10 min)
Shows WriteString, buffered writers and the Flush call people forget.
- Build a line filter over stdin (tutorial, Go by Example, 10 min)
A complete stdin-to-stdout filter: the shape of most Unix-style Go tools.
- Wrap a stream with gzip (reference, Go standard library, 15 min)
gzip.NewReader and NewWriter show how decorators stack on any Reader or Writer.
- Parse CSV records from any reader (reference, Go standard library, 15 min)
csv.Reader consumes any io.Reader, so the same code reads files, HTTP bodies or gzip streams.