Package loader loads, parses and type-checks packages of Go code plus their transitive closure, and retains both the ASTs and the derived facts.


The package defines two primary types: Config, which specifies a set of initial packages to load and various other options; and Program, which is the result of successfully loading the packages specified by a configuration.

The configuration can be set directly, but *Config provides various convenience methods to simplify the common cases, each of which can be called any number of times. Finally, these are followed by a call to Load() to actually load and type-check the program.

var conf loader.Config

// Use the command-line arguments to specify
// a set of initial packages to load from source.
// See FromArgsUsage for help.
rest, err := conf.FromArgs(os.Args[1:], wantTests)

// Parse the specified files and create an ad-hoc package with path "foo".
// All files must have the same 'package' declaration.
err := conf.CreateFromFilenames("foo", "foo.go", "bar.go")

// Create an ad-hoc package with path "foo" from
// the specified already-parsed files.
// All ASTs must have the same 'package' declaration.
err := conf.CreateFromFiles("foo", parsedFiles)

// Add "runtime" to the set of packages to be loaded.

// Adds "fmt" and "fmt_test" to the set of packages
// to be loaded.  "fmt" will include *_test.go files.
err := conf.ImportWithTests("fmt")

// Finally, load all the packages specified by the configuration.
prog, err := conf.Load()


An AD-HOC package is one specified as a set of source files on the command line. In the simplest case, it may consist of a single file such as $GOROOT/src/net/http/triv.go.

EXTERNAL TEST packages are those comprised of a set of *_test.go files all with the same 'package foo_test' declaration, all in the same directory. (go/build.Package calls these files XTestFiles.)

An IMPORTABLE package is one that can be referred to by some import spec. The Path() of each importable package is unique within a Program.

Ad-hoc packages and external test packages are NON-IMPORTABLE. The Path() of an ad-hoc package is inferred from the package declarations of its files and is therefore not a unique package key. For example, Config.CreatePkgs may specify two initial ad-hoc packages both called "main".

An AUGMENTED package is an importable package P plus all the *_test.go files with same 'package foo' declaration as P. (go/build.Package calls these files TestFiles.)

The INITIAL packages are those specified in the configuration. A DEPENDENCY is a package loaded to satisfy an import in an initial package or another dependency.

loader is referenced in 0 repositories