importer
importer for go/types package
It makes go/types.Config.Check usable with imported packages
that have not been installed yet (fixes https://github.com/golang/go/issues/14496)
Documentation
see http://godoc.org/github.com/metakeule/importer
Example
package main
import (
"fmt"
"github.com/metakeule/importer"
"go/ast"
"go/build"
"go/token"
"go/types"
"os"
"path/filepath"
)
func mkInfo() types.Info {
return types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
}
}
func mkConfig() types.Config {
return types.Config{
IgnoreFuncBodies: true,
}
}
func panicOnErr(err error) {
if err != nil {
panic(err.Error())
}
}
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())
}
}
}