Hello everyone,
I am trying to use
golang.org/x/tools/go/ssa to get the methods list of each struct type when doing static analysis on Kubernetes.
This is the code I wrote to print out the method sets for each struct or interface type.
cfg := packages.Config{Mode: packages.LoadAllSyntax}
initial, err := packages.Load(&cfg, pattern)
if err != nil {
log.Fatal(err)
}
prog, _ := ssautil.AllPackages(initial, 0)
prog.Build()
for _, pkg := range prog.AllPackages() {
if pkg.Pkg.Path() == pattern {
for _, member := range pkg.Members {
fmt.Println(member)
if m, ok := member.(*ssa.Type); ok {
fmt.Println(prog.MethodSets.MethodSet(m.Type()))
}
}
}
}I checked everything printed by fmt.Println(member) but it only contains the pkg-level functions instead of struct methods.
I also checked everything printed by fmt.Println(prog.MethodSets.MethodSet(m.Type())) and the method set for each struct/interface only contains the abstract methods and their implementations. Many other methods (which are not the implementation of some abstract methods) are missing here.
In one word, I am able to get all the pkg-level functions in the program but I am NOT able the get the complete method list of any struct type in the program.
Here is what I want:
I want to get the complete method list (a list of *ssa.Function pointing to the method) by calling some API with the struct type (or name)
Thank you so much!