diff --git a/go/gcexportdata/example_test.go b/go/gcexportdata/example_test.go index 7df05abae6a..e81e705b1c4 100644 --- a/go/gcexportdata/example_test.go +++ b/go/gcexportdata/example_test.go @@ -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 }