github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/packages/example_test.go (about) 1 // Copyright 2018 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 package packages_test 6 7 import ( 8 "flag" 9 "fmt" 10 "os" 11 12 "github.com/powerman/golang-tools/go/packages" 13 ) 14 15 // Example demonstrates how to load the packages specified on the 16 // command line from source syntax. 17 func Example() { 18 flag.Parse() 19 20 // Many tools pass their command-line arguments (after any flags) 21 // uninterpreted to packages.Load so that it can interpret them 22 // according to the conventions of the underlying build system. 23 cfg := &packages.Config{Mode: packages.NeedFiles | packages.NeedSyntax} 24 pkgs, err := packages.Load(cfg, flag.Args()...) 25 if err != nil { 26 fmt.Fprintf(os.Stderr, "load: %v\n", err) 27 os.Exit(1) 28 } 29 if packages.PrintErrors(pkgs) > 0 { 30 os.Exit(1) 31 } 32 33 // Print the names of the source files 34 // for each package listed on the command line. 35 for _, pkg := range pkgs { 36 fmt.Println(pkg.ID, pkg.GoFiles) 37 } 38 }