liveBook · Manning

Bufio.Scanner

As explained in Go in Action, Second Edition

bufio.Scanner is a Go type that incrementally reads input and returns successive tokens according to a configured split function.

bufio.Scanner incrementally reads a data source as tokens, avoiding the need to load an entire file into memory. Its default split function returns newline-delimited lines without trailing newline characters. section 2.8 section 2.8.1

Use the Scanner API around a reader:

  • Create one with bufio.NewScanner and the input source, such as a file or os.Stdin. section 2.8.1
  • Call Scan repeatedly; each successful call advances to the next token. section 2.8.1
  • Read the current token with Text, or use Bytes when a byte slice is more suitable. section 2.8.1
  • After scanning stops, check Err; end-of-file is reported as nil, while other errors are returned. section 2.8.1
  • Select a predefined split function or provide a custom one for lines, bytes, UTF-8 runes, words, or another token format. section 2.8.1

Count words line by line

scanner := bufio.NewScanner(os.Stdin) var wordCount int for scanner.Scan() { wordCount += len(strings.Fields(scanner.Text())) } if err := scanner.Err(); err != nil { fmt.Fprintln(os.Stderr, "reading standard input:", err) } section 2.8.1 section 2.8.1

Listing 2.36 Output of go doc bufio Scanner
type Scanner struct {
        // Has unexported fields.
}
    Scanner provides a convenient interface for reading data such as a file
    of newline-delimited lines of text. Successive calls to the Scan method
    will step through the 'tokens' of a file, skipping the bytes between the
    tokens. The specification of a token is defined by a split function of
    type SplitFunc; the default split function breaks the input into lines
    with line termination stripped. Split functions are defined in this
    package for scanning a file into lines, bytes, UTF-8-encoded runes, and
    space-delimited words. The client may instead provide a custom split
    function.

    Scanning stops unrecoverably at EOF, the first I/O error, or a token too
    large to fit in the buffer. When a scan stops, the reader may have
    advanced arbitrarily far past the last token. Programs that need more
    control over error handling or large tokens, or must run sequential
    scans on a reader, should use bufio.Reader instead.

func NewScanner(r io.Reader) *Scanner
func (s *Scanner) Buffer(buf []byte, max int)
func (s *Scanner) Bytes() []byte
func (s *Scanner) Err() error
func (s *Scanner) Scan() bool
func (s *Scanner) Split(split SplitFunc)
func (s *Scanner) Text() string

The go doc listing summarizes Scanner's token model, default split behavior, stopping conditions, and buffer-related failure mode. section 2.8.1

How Go in Action, Second Edition covers it

How deep it goes
Explains construction, token iteration, split functions, result access, errors, and stopping conditions, without implementing scanner internals.
Examples
Demonstrates printing standard-input tokens and counting words with strings.Fields inside the scan loop.
What it assumes
Assumes familiarity with Go packages, functions, loops, files or standard input, and basic error handling.
What you can do after
Apply it via Go's bufio package to scan line-oriented input and detect non-EOF read failures.
How it connects
Connects buffered I/O with readers, split functions, tokens, standard input, files, EOF, and error handling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.