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