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.
12 "golang.org/x/tools/go/packages"
15 // Example demonstrates how to load the packages specified on the
16 // command line from source syntax.
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()...)
26 fmt.Fprintf(os.Stderr, "load: %v\n", err)
29 if packages.PrintErrors(pkgs) > 0 {
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)