Stream data with Go readers and writers

8 steps · about 1 h 55 min · Intermediate

  1. 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.

  2. 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.

  3. 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.

  4. Read files in chunks and with seeks (tutorial, Go by Example, 10 min)

    Short annotated program showing os.Open, Read, Seek and bufio together.

  5. Write files and flush buffered output (tutorial, Go by Example, 10 min)

    Shows WriteString, buffered writers and the Flush call people forget.

  6. 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.

  7. Wrap a stream with gzip (reference, Go standard library, 15 min)

    gzip.NewReader and NewWriter show how decorators stack on any Reader or Writer.

  8. 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.