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