Go Bufio Package
The bufio package (short for "Buffered I/O") provides a way to read and write data efficiently by using a buffer. Instead of making many small, slow requests to the disk or network, bufio groups them into one large batch.
1. Reading with Scanner
The Scanner is the easiest way to read text line-by-line.
2. Reading with Reader
If you need more control (like reading up to a specific character), use bufio.Reader.
3. Writing with Writer
Buffered writing is much faster than standard writing. Note: You must call Flush() at the end to make sure all data is actually written.
Why use bufio?
- Performance: Significantly reduces the number of expensive system calls.
- Convenience: Provides high-level functions like
ReadString,ReadBytes, andScan. - Correctness: Handles complex split logic (like finding where one word ends and another begins) for you.