go-parse-utils is a collection of utilities for parsing code easily.
go get gopkg.in/src-d/go-parse-utils.v1
PackageAST retrieves the *ast.Package of a package in the given path.
pkg, err := parseutil.PackageAST("github.com/my/project")The default importer.Importer of the Go standard library scans compiled objects, which can be painful to deal with in code generation, as it requires to go build before running go generate.
This packages provides an implementation of importer.Importer and importer.ImporterFrom that reads directly from source code if the package is in the GOPATH, otherwise (the stdlib, for example) falls back to the default importer in the standard library.
Features:
- It is safe for concurrent use.
- Caches packages after they are first imported.
importer := parseutil.NewImporter()
pkg, err := importer.Import("github.com/my/project")MIT, see LICENSE