| // Copyright 2018 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| "golang.org/x/tools/go/packages" |
| // Example demonstrates how to load the packages specified on the |
| // command line from source syntax. |
| // Many tools pass their command-line arguments (after any flags) |
| // uninterpreted to packages.Load so that it can interpret them |
| // according to the conventions of the underlying build system. |
| cfg := &packages.Config{Mode: packages.NeedFiles | packages.NeedSyntax} |
| pkgs, err := packages.Load(cfg, flag.Args()...) |
| fmt.Fprintf(os.Stderr, "load: %v\n", err) |
| if packages.PrintErrors(pkgs) > 0 { |
| // Print the names of the source files |
| // for each package listed on the command line. |
| for _, pkg := range pkgs { |
| fmt.Println(pkg.ID, pkg.GoFiles) |