Skip to content

Commit

Permalink
go/gcexportdata: don't assume that fmt names never change
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ianlancetaylor authored and gopherbot committed May 17, 2022
1 parent 1e55371 commit 814e0d7
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions go/gcexportdata/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,37 @@ func ExampleRead() {
log.Fatal(err)
}

// Print package information.
// We can see all the names in Names.
members := pkg.Scope().Names()
if members[0] == ".inittask" {
// An improvement to init handling in 1.13 added ".inittask". Remove so go >= 1.13 and go < 1.13 both pass.
members = members[1:]
foundPrintln := false
for _, member := range members {
if member == "Println" {
foundPrintln = true
break
}
}
fmt.Printf("Package members: %s...\n", members[:5])
fmt.Print("Package members: ")
if foundPrintln {
fmt.Println("Println found")
} else {
fmt.Println("Println not found")
}

// We can also look up a name directly using Lookup.
println := pkg.Scope().Lookup("Println")
posn := fset.Position(println.Pos())
posn.Line = 123 // make example deterministic
typ := strings.ReplaceAll(println.Type().String(), "interface{}", "any") // go 1.18+ uses the 'any' alias
// go 1.18+ uses the 'any' alias
typ := strings.ReplaceAll(println.Type().String(), "interface{}", "any")
fmt.Printf("Println type: %s\n", typ)
posn := fset.Position(println.Pos())
// make example deterministic
posn.Line = 123
fmt.Printf("Println location: %s\n", slashify(posn))

// Output:
//
// Package path: fmt
// Export data: fmt.a
// Package members: [Errorf Formatter Fprint Fprintf Fprintln]...
// Package members: Println found
// Println type: func(a ...any) (n int, err error)
// Println location: $GOROOT/src/fmt/print.go:123:1
}
Expand Down

0 comments on commit 814e0d7

Please sign in to comment.