Go scanner: tokenize Go source text with repeated Scan calls
Answer: The go/scanner package tokenizes Go source text from a byte slice through repeated Scan calls after Init.
Setup
Init prepares a Scanner with a byte slice of source, a file position from go/token, and an optional ErrorHandler. If a syntax error occurs and a handler is set, it is called with a position at the start of the offending token.
ErrorCount is incremented for each error. The mode parameter determines how comments are handled.
Scanning
Repeated Scan calls return the next token. The source end is indicated by EOF. If Scan has not been called yet, End returns NoPos.
In all other cases, Scan returns an empty literal string. If a token ends in '=', the result is tok1 or tok3 respectively.
See also
Claims
- id: C1 claim: The package implements a scanner for Go source text. source: S2 evidence: "Package scanner implements a scanner for Go source text."
- id: C2 claim: The source is a byte slice that is tokenized by repeated Scan calls. source: S2 evidence: "It takes a []byte as source which can then be tokenized"
- id: C3 claim: An ErrorHandler may be provided to Scanner.Init. source: S1 evidence: "An ErrorHandler may be provided to"
Sources
- [S1] go/scanner. The Go Authors. https://pkg.go.dev/go/scanner . License: BSD-3-Clause. Accessed 2026-11-26.
- [S2] go/scanner source. The Go Authors. https://go.dev/src/go/scanner/scanner.go . License: BSD-3-Clause. Accessed 2026-11-26.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Go Authors, go/scanner; The Go Authors, go/scanner source. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Go Authors, go/scanner; The Go Authors, go/scanner source. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors.
Cite this
iinventedlight. 2026. "Go scanner: tokenize Go source text with repeated Scan calls." iinventedlight Library, iil:note:tbja8w3j, version of 2026-11-26. https://iinventedlight.com/library/notes/go-scanner/. License: CC-BY-4.0.
@misc{iil_note_tbja8w3j,
author = {{iinventedlight}},
title = {{Go scanner: tokenize Go source text with repeated Scan calls}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-26},
url = {https://iinventedlight.com/library/notes/go-scanner/},
note = {Permanent ID iil:note:tbja8w3j. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- go-scanner.schema.json (schema; CC-BY-4.0)
- go-scanner.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-scanner.md.