Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Errors, I/O and Files

Go bufio package

Reference for the Go bufio package: Scanner, Reader and Writer, ReadString, ReadLine, Flush, buffer sizes, split functions and common buffering mistakes.

The package bufio adds buffering to readers and writers. Instead of making a system call for every small read or write, a buffered reader fetches a large block at once and hands out pieces, and a buffered writer collects small writes and sends them in one go. That is often much faster, and it also adds convenience methods such as reading a line or a word.

It has three main types. bufio.Scanner reads lines or words with a simple loop. bufio.Reader gives finer control (ReadString, ReadRune, Peek). bufio.Writer batches output and needs a final Flush.

Go
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
	fmt.Println(sc.Text())
}

Try it

Go Reading lines and words

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go A buffered writer needs Flush

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
func NewReader(rd io.Reader) *Reader
func NewReaderSize(rd io.Reader, size int) *Reader
func NewWriter(w io.Writer) *Writer
func NewWriterSize(w io.Writer, size int) *Writer
func NewScanner(r io.Reader) *Scanner
func NewReadWriter(r *Reader, w *Writer) *ReadWriter
Type Main methods
Scanner Scan, Text, Bytes, Err, Buffer, Split
Reader Read, ReadByte, ReadRune, ReadString, ReadBytes, ReadLine, Peek, Discard, Buffered
Writer Write, WriteString, WriteByte, WriteRune, Flush, Buffered, Available

Split functions for Scanner: ScanLines (default), ScanWords, ScanRunes, ScanBytes.

Defaults: readers and writers use a 4096-byte buffer. A Scanner allows tokens up to 64 KB.

Usage notes

  • Always Flush a Writer. Data stays in the buffer until it fills or you flush. Forgetting this is the most common bug; use defer w.Flush() in main (and handle its error when writing files).
  • Scanner token limit. A line longer than 64 KB makes Scan return false with bufio.ErrTooLong. Call sc.Buffer(make([]byte, 1024*1024), 1024*1024) before scanning to raise it, or use Reader.ReadString.
  • Check sc.Err() after the loop. Scan returns false for both a clean end and a failure; Err tells them apart (it is nil at EOF).
  • Text vs Bytes: Text allocates a string; Bytes returns a slice that is overwritten by the next Scan.
  • Speed for stdin and stdout. For heavy output in contests or batch tools, wrap os.Stdout in a bufio.Writer; for big inputs wrap os.Stdin in a bufio.Reader.
  • Do not mix a buffered reader with the raw source. The buffer may already hold data that the underlying reader will never return.
  • ReadString returns the delimiter and returns partial data plus io.EOF for a last line without a newline.
  • Not thread-safe. Guard with a mutex if goroutines share a buffered writer.