Debian Codegraph @

go/gcexportdata/example_test.go

raw symbols SCIP
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build go1.7 && gc && !android && !ios && (unix || aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || plan9 || windows)
6
7 package gcexportdata_test
8
9 import (
10 "fmt"
11 "go/ast"
12 "go/parser"
13 "go/token"
14 "go/types"
15 "log"
16 "os"
17 "path/filepath"
18 "slices"
19 "strings"
20
21 "golang.org/x/tools/go/gcexportdata"
22 )
23
24 // ExampleRead uses gcexportdata.Read to load type information for the
25 // "fmt" package from the fmt.a file produced by the gc compiler.
26 func ExampleRead() {
27 // Find the export data file.
28 filename, path := gcexportdata.Find("fmt", "")
29 if filename == "" {
30 log.Fatalf("can't find export data for fmt")
31 }
32 fmt.Printf("Package path: %s\n", path)
33
34 // Open and read the file.
35 f, err := os.Open(filename)
36 if err != nil {
37 log.Fatal(err)
38 }
39 defer f.Close()
40 r, err := gcexportdata.NewReader(f)
41 if err != nil {
42 log.Fatalf("reading export data %s: %v", filename, err)
43 }
44
45 // Decode the export data.
46 fset := token.NewFileSet()
47 imports := make(map[string]*types.Package)
48 pkg, err := gcexportdata.Read(r, fset, imports, path)
49 if err != nil {
50 log.Fatal(err)
51 }
52
53 // We can see all the names in Names.
54 members := pkg.Scope().Names()
55 foundPrintln := slices.Contains(members, "Println")
56 fmt.Print("Package members: ")
57 if foundPrintln {
58 fmt.Println("Println found")
59 } else {
60 fmt.Println("Println not found")
61 }
62
63 // We can also look up a name directly using Lookup.
64 println := pkg.Scope().Lookup("Println")
65 // go 1.18+ uses the 'any' alias
66 typ := strings.ReplaceAll(println.Type().String(), "interface{}", "any")
67 fmt.Printf("Println type: %s\n", typ)
68 posn := fset.Position(println.Pos())
69 // make example deterministic
70 posn.Line = 123
71 fmt.Printf("Println location: %s\n", slashify(posn))
72
73 // Output:
74 //
75 // Package path: fmt
76 // Package members: Println found
77 // Println type: func(a ...any) (n int, err error)
78 // Println location: $GOROOT/src/fmt/print.go:123:1
79 }
80
81 // ExampleNewImporter demonstrates usage of NewImporter to provide type
82 // information for dependencies when type-checking Go source code.
83 func ExampleNewImporter() {
84 const src = `package myrpc
85
86 // choosing a package that doesn't change across releases
87 import "net/rpc"
88
89 const serverError rpc.ServerError = ""
90 `
91 fset := token.NewFileSet()
92 f, err := parser.ParseFile(fset, "myrpc.go", src, 0)
93 if err != nil {
94 log.Fatal(err)
95 }
96
97 packages := make(map[string]*types.Package)
98 imp := gcexportdata.NewImporter(fset, packages)
99 conf := types.Config{Importer: imp}
100 pkg, err := conf.Check("myrpc", fset, []*ast.File{f}, nil)
101 if err != nil {
102 log.Fatal(err)
103 }
104
105 // object from imported package
106 pi := packages["net/rpc"].Scope().Lookup("ServerError")
107 fmt.Printf("type %s.%s %s // %s\n",
108 pi.Pkg().Path(),
109 pi.Name(),
110 pi.Type().Underlying(),
111 slashify(fset.Position(pi.Pos())),
112 )
113
114 // object in source package
115 twopi := pkg.Scope().Lookup("serverError")
116 fmt.Printf("const %s %s = %s // %s\n",
117 twopi.Name(),
118 twopi.Type(),
119 twopi.(*types.Const).Val(),
120 slashify(fset.Position(twopi.Pos())),
121 )
122
123 // Output:
124 //
125 // type net/rpc.ServerError string // $GOROOT/src/net/rpc/client.go:20:1
126 // const serverError net/rpc.ServerError = "" // myrpc.go:6:7
127 }
128
129 func slashify(posn token.Position) token.Position {
130 posn.Filename = filepath.ToSlash(posn.Filename) // for MS Windows portability
131 return posn
132 }