I'm trying to call functions that are not bound to any form of struct using reflection which doesn't seem to be possible.
I keep running into the issue of having to call reflect.ValueOf(T)
which isn't possible since the functions are not bound to any form of struct.
// main.go
const subPackage = "subPackage"
func main() {
set := token.NewFileSet()
packs, err := parser.ParseDir(set, subPackage, nil, 0)
if err != nil {
fmt.Println("failed to parse package:", err)
os.Exit(1)
}
var funcs []*ast.FuncDecl
for _, pack := range packs {
for _, f := range pack.Files {
for _, d := range f.Decls {
if fn, isFn := d.(*ast.FuncDecl); isFn {
funcs = append(funcs, fn)
reflect.ValueOf(T)MethodByName(fn.Name.Name) // not possible since T would need to be a struct!
}
}
}
}
}
// subPackage/test.go
func testFunc() {
fmt.Println("hello world")
}
The code currently just parses the directory subPackage
and gets all the root-level function declarations, I'm just not sure how to go about calling the unbound functions without a struct.
Aucun commentaire:
Enregistrer un commentaire