Radar · Go · Archive · Week 46 · Nov 10 – 16, 2025
go/token: add File.End method
Worth knowingstdlib
- What changed
- The
Filetype in thego/tokenpackage now has anEnd()method that returns the position of the end of the file. - Production impact
- The source does not say.
- Try it
- Import
go/tokenand callf.End()on atoken.File. - Source
- github.com/golang/go/issues/75849
Understand it, then run it
The go/token package keeps track of where text appears in a source file. A File value knows its start position (Base) and its length (Size). Before this change you had to compute the end of the file by adding those two numbers every time. The new End() method does that for you, returning the position just after the last byte of the file. It saves a few keystrokes and makes the intent clearer.
Run it now
// This program demonstrates the current state of the go/token File type in Go 1.27.1.
// The proposed End() method is not yet available, so we compute the end position
// manually using Base() and Size() and print the result.
package main
import (
"fmt"
"go/token"
)
func main() {
// Create a new FileSet and add a file named "example.go" with 20 bytes of content.
fset := token.NewFileSet()
f := fset.AddFile("example.go", -1, 20)
// Base() returns the starting position of the file (usually 1).
base := f.Base()
// Size() returns the number of bytes in the file.
size := f.Size()
// Compute the end position as Base() + Size().
end := token.Pos(base + size)
fmt.Printf("Base: %d\n", base)
fmt.Printf("Size: %d\n", size)
fmt.Printf("End: %d\n", end)
}
What it printed when we ran it on Go 1.27.1
Base: 1 Size: 20 End: 21
After the change ships
go · the proposal's code; it does not compile until the change ships
// This does not compile until the change ships in Go 1.27.1.
package main
import (
"fmt"
"go/token"
)
func main() {
fset := token.NewFileSet()
f := fset.AddFile("example.go", -1, 20)
fmt.Println("End:", f.End())
}
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