Bufio Scanner And Writer
Fast recall
Quick revision
Core answer
Whether you understand Bufio Scanner And Writer beyond memorized syntax.
Giving a definition without explaining a practical trade-off.
Connect the concept to a small production example and state its complexity or failure mode.
Interview-ready guide
Interview answer
Both, executed:
// Scanner — line-by-line reading
scanner := bufio.NewScanner(strings.NewReader("line one\nline two\nline three\n"))
for scanner.Scan() {
fmt.Println("scanned:", scanner.Text())
}
// line one, line two, line three — verified
// Writer — buffered writing
var buf strings.Builder
w := bufio.NewWriter(&buf)
w.WriteString("buffered ")
w.WriteString("output\n")
w.Flush() // without Flush, the data never arrives
fmt.Print(buf.String()) // "buffered output\n" — verifiedThe Scanner pattern: NewScanner(io.Reader) → Scan() returns bool (false at EOF) → Text() returns the current line. It handles line splitting (\n by default; custom split functions for words, CSV, etc.). The loop is the idiom — a for with Scan and a body using Text/Bytes.
The Flush discipline: a bufio.Writer holds a buffer (default 4096 bytes). Writes accumulate; Flush pushes them to the underlying writer. If you forget Flush, partial data stays in the buffer — the classic bug. defer w.Flush() after creating the writer.
The why: batching. A hundred 100-byte writes to a file or network are 100 syscall-sized operations — buffered, they're one 10KB write. The performance difference is an order of magnitude on most systems.
Bufio Scanner And Writer
Direct Answer
Scanner: reads a stream line-by-line (Scan() advances, Text() returns the line). Writer: buffers writes (WriteString accumulates, Flush commits) — batching small writes into large I/O operations.
Deep Dive & Architecture
The Scanner's internal buffer and the line-length limit: Scanner has a fixed 64KB default buffer — a line longer than the limit makes Scan return false with Err(too long). The fix: scanner.Buffer(make([]byte, 10241024), 10241024) for large lines. The common production bug: log files with huge lines failing silently.
The split functions: Scan() uses SplitFunc — the default ScanLines; bufio.ScanWords, bufio.ScanRunes exist; a custom SplitFunc handles any framing. The Scanner is a general tokenizer over an io.Reader.
The Writer's sizing: NewWriterSize(w, n) for a custom buffer. The trade: larger buffers mean fewer, larger writes (good for throughput) with more memory and latency (data sits until the buffer fills). The TSV of batching — tuned by the actual write pattern.
Common Mistakes & Pitfalls
Giving a definition without explaining a practical trade-off.
Check your understanding
Answer these without looking back at the article.
- When would you use Bufio Scanner And Writer in a real project?
- How would you compare Bufio Scanner And Writer with an alternative?
- What is a common mistake when using Bufio Scanner And Writer?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.