@@ -51,25 +51,37 @@ func ExampleRead() {
51
51
log .Fatal (err )
52
52
}
53
53
54
- // Print package information .
54
+ // We can see all the names in Names .
55
55
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
+ }
59
62
}
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.
61
71
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" )
65
74
fmt .Printf ("Println type: %s\n " , typ )
75
+ posn := fset .Position (println .Pos ())
76
+ // make example deterministic
77
+ posn .Line = 123
66
78
fmt .Printf ("Println location: %s\n " , slashify (posn ))
67
79
68
80
// Output:
69
81
//
70
82
// Package path: fmt
71
83
// Export data: fmt.a
72
- // Package members: [Errorf Formatter Fprint Fprintf Fprintln]...
84
+ // Package members: Println found
73
85
// Println type: func(a ...any) (n int, err error)
74
86
// Println location: $GOROOT/src/fmt/print.go:123:1
75
87
}
0 commit comments