Radar · Go · Archive · Week 5 · Jan 26 – Feb 1, 2026
go/scanner: add (*Scanner).End()
Worth knowingstdlib
- What changed
- The
go/scannerpackage now has anEnd()method that returns the position immediately after the last scanned token, ortoken.NoPosifScanhas not been called. - Production impact
- The source does not say.
- Try it
- Initialize a
scanner.Scanner, callScan()a few times, then calls.End()to get the end position. - Source
- github.com/golang/go/issues/74958
Understand it, then run it
Run it now
// This program demonstrates the new (*Scanner).End() method.
// It scans a simple Go source string and prints the position after the last token.
package main
import (
"fmt"
"go/scanner"
"go/token"
)
func main() {
src := []byte("package main\nfunc main() {}")
fset := token.NewFileSet()
file := fset.AddFile("", fset.Base(), len(src))
var s scanner.Scanner
s.Init(file, src, nil, scanner.ScanComments)
// Scan all tokens.
for {
pos, tok, lit := s.Scan()
if tok == token.EOF {
break
}
fmt.Printf("token: %s, pos: %v, lit: %q\n", tok, pos, lit)
}
// After scanning, End() gives the position after the last token.
endPos := s.End()
fmt.Printf("End position: %v (offset %d)\n", endPos, file.Offset(endPos))
}
What it printed when we ran it on Go 1.27.1
token: package, pos: 1, lit: "package"
token: IDENT, pos: 9, lit: "main"
token: ;, pos: 13, lit: "\n"
token: func, pos: 14, lit: "func"
token: IDENT, pos: 19, lit: "main"
token: (, pos: 23, lit: ""
token: ), pos: 24, lit: ""
token: {, pos: 26, lit: ""
token: }, pos: 27, lit: ""
token: ;, pos: 28, lit: "\n"
End position: 28 (offset 27)
Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed