liveBook · Manning

Buffering

As explained in Go in Action, Second Edition

Buffering is the technique of temporarily accumulating input or output data in memory to reduce the cost of individual transfers.

Buffering temporarily accumulates input or output data in memory, reducing the cost of individual transfers. In Go, the bufio package wraps I/O operations so programs can process data in manageable chunks instead of loading an entire file at once. section 2.8 section 10.6

Use the bufio types according to the operation:

  • bufio.Reader buffers an io.Reader, supports small reads, line-oriented processing, and look-ahead with Peek. section 10.6
  • bufio.Scanner supplies input a piece at a time and can split data in several ways. section 2.8
  • bufio.Writer batches writes to an io.Writer; call Flush to send remaining buffered data. section 10.6

Line processing and format detection

A line processor can call ReadString repeatedly and handle the final unterminated line at io.EOF. A format detector can call Peek(1) before consuming input, recognizing JSON from { or [ and XML from <.

Buffered conversion output

A JSON/XML conversion tool can wrap its output with bufio.NewWriter, write converted data, and flush before returning. Buffering reduces system-call overhead, while explicit flush handling prevents pending output from being lost.

How Go in Action, Second Edition covers it

How deep it goes
Explains buffering mechanics, bufio wrappers, line reads, peeking, writes, flushing, and practical conversion-tool integration.
Examples
Demonstrates line-by-line reading, non-consuming format detection, buffered writing, and JSON/XML conversion.
What it assumes
Assumes familiarity with Go I/O and introduces bufio as a layer over io.Reader and io.Writer.
What you can do after
Apply buffering via Go's bufio package to process input incrementally and write output efficiently.
How it connects
Connects io.Reader and io.Writer with bufio.Reader, bufio.Writer, bufio.Scanner, file processing, format detection, and I/O performance.

Also explained in

Compare how 5 books explain buffering →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.