Go Reference ยท Errors, I/O and Files
Go bufio.Scanner type
Reference for Go bufio.Scanner: Scan, Text, Bytes, Err, Split with ScanWords and ScanRunes, custom split functions, the 64 KB token limit and Buffer.
A bufio.Scanner reads input one token at a time. By default a token is a line, but you can switch to words, runes or bytes, or write your own split function. The loop for sc.Scan() { ... } is the usual way to process a file, standard input or any other reader line by line.
The scanner hides buffering and the end-of-line details: Text returns the line without the trailing newline (and without a trailing \r). When Scan returns false, the input is exhausted or an error occurred, and Err tells you which.
sc := bufio.NewScanner(r) for sc.Scan() { use(sc.Text()) } if err := sc.Err(); err != nil { return err }
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 NewScanner(r io.Reader) *Scanner func (s *Scanner) Scan() bool func (s *Scanner) Text() string func (s *Scanner) Bytes() []byte func (s *Scanner) Err() error func (s *Scanner) Buffer(buf []byte, max int) func (s *Scanner) Split(split SplitFunc) type SplitFunc func(data []byte, atEOF bool) (advance int, token []byte, err error)
| Name | Description |
|---|---|
Scan |
Advances to the next token; false at the end or on an error. |
Text, Bytes |
The current token as a string or as a slice that the next Scan may overwrite. |
Err |
The first non-EOF error, or nil. |
Buffer(buf, max) |
Sets the initial buffer and the largest token size. Call before the first Scan. |
Split(f) |
Chooses the tokenizer: ScanLines, ScanWords, ScanRunes, ScanBytes or your own. Call before the first Scan. |
Errors: bufio.ErrTooLong when a token exceeds the buffer limit (64 KB by default); bufio.ErrFinalToken can be returned by a split function to end scanning; panics if Split or Buffer is called after scanning started.
Usage notes
- Check
Err()after the loop.Scanalone cannot distinguish the end of the input from a failure. - The 64 KB limit is the classic trap. Long lines (minified JSON, DNA data) stop the scan silently unless you check
Err()or raise the limit withBuffer. - Empty lines are tokens. The line
""is returned as an empty string; skip withif line == "". ScanWordsuses Unicode white space, so tabs and newlines both separate words. It is a fast way to read many numbers.- Copy
Bytes()if you keep it. The slice points into the scanner's buffer and changes on the nextScan;Text()returns an independent string. - Interactive input: with
os.Stdinthe loop ends at EOF (Ctrl+D on Unix, Ctrl+Z on Windows). - Custom splitters return how far to advance and the token; return
0, nil, nilto request more data, as inscanCSV. - For huge lines or exact control, use
bufio.Reader.ReadStringinstead.