github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/go/loader/example_test.go (about) 1 // Copyright 2015 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 // +build go1.6 6 // +build !windows 7 8 package loader_test 9 10 import ( 11 "fmt" 12 "go/token" 13 "log" 14 "path/filepath" 15 "runtime" 16 "sort" 17 "strings" 18 19 "golang.org/x/tools/go/loader" 20 ) 21 22 func printProgram(prog *loader.Program) { 23 // Created packages are the initial packages specified by a call 24 // to CreateFromFilenames or CreateFromFiles. 25 var names []string 26 for _, info := range prog.Created { 27 names = append(names, info.Pkg.Path()) 28 } 29 fmt.Printf("created: %s\n", names) 30 31 // Imported packages are the initial packages specified by a 32 // call to Import or ImportWithTests. 33 names = nil 34 for _, info := range prog.Imported { 35 if strings.Contains(info.Pkg.Path(), "internal") { 36 continue // skip, to reduce fragility 37 } 38 names = append(names, info.Pkg.Path()) 39 } 40 sort.Strings(names) 41 fmt.Printf("imported: %s\n", names) 42 43 // InitialPackages contains the union of created and imported. 44 names = nil 45 for _, info := range prog.InitialPackages() { 46 names = append(names, info.Pkg.Path()) 47 } 48 sort.Strings(names) 49 fmt.Printf("initial: %s\n", names) 50 51 // AllPackages contains all initial packages and their dependencies. 52 names = nil 53 for pkg := range prog.AllPackages { 54 names = append(names, pkg.Path()) 55 } 56 sort.Strings(names) 57 fmt.Printf("all: %s\n", names) 58 } 59 60 func printFilenames(fset *token.FileSet, info *loader.PackageInfo) { 61 var names []string 62 for _, f := range info.Files { 63 names = append(names, filepath.Base(fset.File(f.Pos()).Name())) 64 } 65 fmt.Printf("%s.Files: %s\n", info.Pkg.Path(), names) 66 } 67 68 // This example loads a set of packages and all of their dependencies 69 // from a typical command-line. FromArgs parses a command line and 70 // makes calls to the other methods of Config shown in the examples that 71 // follow. 72 func ExampleConfig_FromArgs() { 73 args := []string{"mytool", "unicode/utf8", "errors", "runtime", "--", "foo", "bar"} 74 const wantTests = false 75 76 var conf loader.Config 77 rest, err := conf.FromArgs(args[1:], wantTests) 78 prog, err := conf.Load() 79 if err != nil { 80 log.Fatal(err) 81 } 82 83 fmt.Printf("rest: %s\n", rest) 84 printProgram(prog) 85 // Output: 86 // rest: [foo bar] 87 // created: [] 88 // imported: [errors runtime unicode/utf8] 89 // initial: [errors runtime unicode/utf8] 90 // all: [errors runtime runtime/internal/atomic runtime/internal/sys unicode/utf8] 91 } 92 93 // This example creates and type-checks a single package (without tests) 94 // from a list of filenames, and loads all of its dependencies. 95 func ExampleConfig_CreateFromFilenames() { 96 var conf loader.Config 97 filename := filepath.Join(runtime.GOROOT(), "src/container/heap/heap.go") 98 conf.CreateFromFilenames("container/heap", filename) 99 prog, err := conf.Load() 100 if err != nil { 101 log.Fatal(err) 102 } 103 104 printProgram(prog) 105 // Output: 106 // created: [container/heap] 107 // imported: [] 108 // initial: [container/heap] 109 // all: [container/heap sort] 110 } 111 112 // In the examples below, for stability, the chosen packages are 113 // relatively small, platform-independent, and low-level (and thus 114 // infrequently changing). 115 // The strconv package has internal and external tests. 116 117 const hello = `package main 118 119 import "fmt" 120 121 func main() { 122 fmt.Println("Hello, world.") 123 } 124 ` 125 126 // This example creates and type-checks a package from a list of 127 // already-parsed files, and loads all its dependencies. 128 func ExampleConfig_CreateFromFiles() { 129 var conf loader.Config 130 f, err := conf.ParseFile("hello.go", hello) 131 if err != nil { 132 log.Fatal(err) 133 } 134 conf.CreateFromFiles("hello", f) 135 prog, err := conf.Load() 136 if err != nil { 137 log.Fatal(err) 138 } 139 140 printProgram(prog) 141 printFilenames(prog.Fset, prog.Package("strconv")) 142 // Output: 143 // created: [hello] 144 // imported: [] 145 // initial: [hello] 146 // all: [errors fmt hello internal/race io math os reflect runtime runtime/internal/atomic runtime/internal/sys strconv sync sync/atomic syscall time unicode/utf8] 147 // strconv.Files: [atob.go atof.go atoi.go decimal.go doc.go extfloat.go ftoa.go isprint.go itoa.go quote.go] 148 } 149 150 // This example imports three packages, including the tests for one of 151 // them, and loads all their dependencies. 152 func ExampleConfig_Import() { 153 // ImportWithTest("strconv") causes strconv to include 154 // internal_test.go, and creates an external test package, 155 // strconv_test. 156 // (Compare with the example of CreateFromFiles.) 157 158 var conf loader.Config 159 conf.Import("unicode/utf8") 160 conf.Import("errors") 161 conf.ImportWithTests("strconv") 162 prog, err := conf.Load() 163 if err != nil { 164 log.Fatal(err) 165 } 166 167 printProgram(prog) 168 printFilenames(prog.Fset, prog.Package("strconv")) 169 printFilenames(prog.Fset, prog.Package("strconv_test")) 170 // Output: 171 // created: [strconv_test] 172 // imported: [errors strconv unicode/utf8] 173 // initial: [errors strconv strconv_test unicode/utf8] 174 // all: [bufio bytes errors flag fmt internal/race io log math math/rand os reflect runtime runtime/debug runtime/internal/atomic runtime/internal/sys runtime/pprof runtime/trace sort strconv strconv_test strings sync sync/atomic syscall testing text/tabwriter time unicode unicode/utf8] 175 // strconv.Files: [atob.go atof.go atoi.go decimal.go doc.go extfloat.go ftoa.go isprint.go itoa.go quote.go internal_test.go] 176 // strconv_test.Files: [atob_test.go atof_test.go atoi_test.go decimal_test.go example_test.go fp_test.go ftoa_test.go itoa_test.go quote_test.go strconv_test.go] 177 }