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.




