func main() { var ( pkgpath = "github.com/metakeule/importer" // assumes a single GOPATH directory srcDir = filepath.Join(os.Getenv("GOPATH"), "src", pkgpath) fset = token.NewFileSet() astFiles []*ast.File ) buildPkg, err := build.Import(pkgpath, srcDir, 0) panicOnErr(err) astFiles, err = importer.ParseAstFiles(fset, buildPkg.Dir, append(buildPkg.GoFiles, buildPkg.CgoFiles...)) panicOnErr(err) info, config := mkInfo(), mkConfig() // here the importer is used with the same config and info settings config.Importer = importer.CheckImporter(mkInfo, mkConfig) _, err = (&config).Check(buildPkg.ImportPath, fset, astFiles, &info) panicOnErr(err) for ident, obj := range info.Defs { if ident.IsExported() { fmt.Printf("%s:\n %s\n", ident.Name, obj.Type()) } } }
// panicks on build errors func (pkg *pack) parse() (err error) { pkg.astFiles, err = importer.ParseAstFiles(pkg.fset, pkg.buildPkg.Dir, append(pkg.buildPkg.GoFiles, pkg.buildPkg.CgoFiles...)) if err != nil { return } tinfo := mkInfo() pkg.typesInfo = &tinfo var conf = mkConfig() conf.Importer = importer.CheckImporter(mkInfo, mkConfig) pkg.typesPkg, err = conf.Check(pkg.buildPkg.ImportPath, pkg.fset, pkg.astFiles, pkg.typesInfo) return }