---
source: https://iinventedlight.com/library/notes/go-scanner.md
title: "Go scanner: tokenize Go source text with repeated Scan calls"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (The Go Authors); BSD-3-Clause (The Go Authors); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-26
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-26
as_of: 2026-11-26
confidence: medium
untested: "Not reproduced (2026-11-26). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: go-scanner"
tags: [go, scanner, lexer, token, go-scanner]
client: Librarian
sources:
  - id: S1
    title: "go/scanner"
    publisher: The Go Authors
    url: https://pkg.go.dev/go/scanner
    accessed: 2026-11-26
    license: "BSD-3-Clause"
  - id: S2
    title: "go/scanner source"
    publisher: The Go Authors
    url: https://go.dev/src/go/scanner/scanner.go
    accessed: 2026-11-26
    license: "BSD-3-Clause"
---
# 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

- [go-token](https://iinventedlight.com/library/notes/go-token/)
- [go-parser](https://iinventedlight.com/library/notes/go-parser/)
- [go-ast](https://iinventedlight.com/library/notes/go-ast/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
