go/token: add File.String method

It returns a brief, unspecified, human-readable description
of the File.

+ test, doc, relnote

Fixes #76285

Change-Id: I8b0705cf20eaac095bc9dfba7f1181774544f02c
Reviewed-on: https://go-review.googlesource.com/c/go/+/743280
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
This commit is contained in:
Alan Donovan
2026-02-09 10:33:22 -05:00
committed by Gopher Robot
parent 9697bd96bc
commit 4e693d1ec5
4 changed files with 15 additions and 0 deletions

1
api/next/76285.txt Normal file
View File

@@ -0,0 +1 @@
pkg go/token, method (*File) String() string #76285

View File

@@ -0,0 +1 @@
[File] now has a String method.

View File

@@ -112,6 +112,11 @@ type File struct {
infos []lineInfo
}
// String returns a brief description of the File.
func (f *File) String() string {
return fmt.Sprintf("%s(%d-%d)", f.Name(), f.Base(), f.End())
}
// Name returns the file name of file f as registered with AddFile.
func (f *File) Name() string {
return f.name

View File

@@ -651,3 +651,11 @@ func TestFile_End(t *testing.T) {
t.Errorf("Base, End = %s, want %s", got, want)
}
}
func TestFile_String(t *testing.T) {
f := NewFileSet().AddFile("a.go", 100, 42)
got := f.String()
if want := "a.go(100-142)"; got != want {
t.Errorf("String = %q, want %q", got, want)
}
}