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.
sc := bufio.NewScanner(os.Stdin) for sc.Scan() { fmt.Println(sc.Text()) }
Try it
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.
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
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
FlushaWriter. Data stays in the buffer until it fills or you flush. Forgetting this is the most common bug; usedefer w.Flush()inmain(and handle its error when writing files). - Scanner token limit. A line longer than 64 KB makes
Scanreturn false withbufio.ErrTooLong. Callsc.Buffer(make([]byte, 1024*1024), 1024*1024)before scanning to raise it, or useReader.ReadString. - Check
sc.Err()after the loop.Scanreturns false for both a clean end and a failure;Errtells them apart (it is nil at EOF). TextvsBytes:Textallocates a string;Bytesreturns a slice that is overwritten by the nextScan.- Speed for stdin and stdout. For heavy output in contests or batch tools, wrap
os.Stdoutin abufio.Writer; for big inputs wrapos.Stdinin abufio.Reader. - Do not mix a buffered reader with the raw source. The buffer may already hold data that the underlying reader will never return.
ReadStringreturns the delimiter and returns partial data plusio.EOFfor a last line without a newline.- Not thread-safe. Guard with a mutex if goroutines share a buffered writer.
Related
bufio.Scanner,bufio.ReaderandWriter,io,os,fmt.Fprintf()- Lessons: os, io and bufio, User input