liveBook · Manning

Token

As explained in Go in Action, Second Edition

A token is a piece of input data produced by a scanner according to its splitting rules.

A token is one piece of input produced by a scanner. In Go, bufio.Scanner uses a split function to determine token boundaries; by default, it returns lines without their trailing newlines. Scan advances to the next token, which Text makes available as a string. 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 Scanner documentation describes default and alternative split rules, including lines, bytes, runes, words, and custom splitting. section 2.8.1

Count words in scanned lines

Scan yields each line by default; strings.Fields then separates that line into words, whose count can be added to a running total. section 2.8.1

How Go in Action, Second Edition covers it

How deep it goes
Defines tokens through Scanner splitting behavior, including newline defaults and alternatives, but does not detail custom split-function implementation.
Examples
Counts whitespace-separated words by applying strings.Fields to each scanned line.
What you can do after
Apply tokens through bufio.Scanner to read input sequentially and process each resulting line.
How it connects
Relates tokens to bufio.Scanner, SplitFunc, Scan, Text, and io.Reader.

Also explained in

Compare how 31 books explain token →

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.