Skip to content

Commit 814e0d7

Browse files
ianlancetaylorgopherbot
authored andcommitted
go/gcexportdata: don't assume that fmt names never change
For golang/go#47579 Change-Id: I25a873fb6da216d885c8faefda98c7fe027b6a4f Reviewed-on: https://go-review.googlesource.com/c/tools/+/406357 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Rob Pike <[email protected]> gopls-CI: kokoro <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 1e55371 commit 814e0d7

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

go/gcexportdata/example_test.go

+21-9
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,37 @@ func ExampleRead() {
5151
log.Fatal(err)
5252
}
5353

54-
// Print package information.
54+
// We can see all the names in Names.
5555
members := pkg.Scope().Names()
56-
if members[0] == ".inittask" {
57-
// An improvement to init handling in 1.13 added ".inittask". Remove so go >= 1.13 and go < 1.13 both pass.
58-
members = members[1:]
56+
foundPrintln := false
57+
for _, member := range members {
58+
if member == "Println" {
59+
foundPrintln = true
60+
break
61+
}
5962
}
60-
fmt.Printf("Package members: %s...\n", members[:5])
63+
fmt.Print("Package members: ")
64+
if foundPrintln {
65+
fmt.Println("Println found")
66+
} else {
67+
fmt.Println("Println not found")
68+
}
69+
70+
// We can also look up a name directly using Lookup.
6171
println := pkg.Scope().Lookup("Println")
62-
posn := fset.Position(println.Pos())
63-
posn.Line = 123 // make example deterministic
64-
typ := strings.ReplaceAll(println.Type().String(), "interface{}", "any") // go 1.18+ uses the 'any' alias
72+
// go 1.18+ uses the 'any' alias
73+
typ := strings.ReplaceAll(println.Type().String(), "interface{}", "any")
6574
fmt.Printf("Println type: %s\n", typ)
75+
posn := fset.Position(println.Pos())
76+
// make example deterministic
77+
posn.Line = 123
6678
fmt.Printf("Println location: %s\n", slashify(posn))
6779

6880
// Output:
6981
//
7082
// Package path: fmt
7183
// Export data: fmt.a
72-
// Package members: [Errorf Formatter Fprint Fprintf Fprintln]...
84+
// Package members: Println found
7385
// Println type: func(a ...any) (n int, err error)
7486
// Println location: $GOROOT/src/fmt/print.go:123:1
7587
}

0 commit comments

Comments
 (0)