cmd/structlayout: port to go/packages
@@ -14,7 +14,7 @@ import (
st "honnef.co/go/tools/structlayout"
"honnef.co/go/tools/version"
- "golang.org/x/tools/go/loader"
+ "golang.org/x/tools/go/packages"
)
var (
@@ -41,24 +41,22 @@ func main() {
os.Exit(1)
}
- conf := loader.Config{
- Build: &build.Default,
+ cfg := &packages.Config{
+ Mode: packages.NeedImports | packages.NeedExportsFile | packages.NeedTypes | packages.NeedSyntax,
+ Tests: true,
}
-
- var pkg string
- var typName string
- pkg = flag.Args()[0]
- typName = flag.Args()[1]
- conf.Import(pkg)
-
- lprog, err := conf.Load()
+ pkgs, err := packages.Load(cfg, flag.Args()[0])
if err != nil {
log.Fatal(err)
}
+
+ for _, pkg := range pkgs {
+ typName := flag.Args()[1]
+
var typ types.Type
- obj := lprog.Package(pkg).Pkg.Scope().Lookup(typName)
+ obj := pkg.Types.Scope().Lookup(typName)
if obj == nil {
- log.Fatal("couldn't find type")
+ continue
}
typ = obj.Type()
@@ -73,6 +71,10 @@ func main() {
} else {
emitText(fields)
}
+ return
+ }
+
+ log.Fatal("couldn't find type")
}
func emitJSON(fields []st.Field) {
