0

How can I create a map from all of the functions found in another file in Go? It's pretty easy to get the names of them using the following code, but I haven't found a way to get the actual functions, or for the desired code to work.

fileSet := token.NewFileSet()
ast, err := parser.ParseFile(fileSet, "funcFile.go", nil, 0)
if err != nil {
    panic(err)
}
for key := range ast.Scope.Objects {
    fmt.Println(key) //
}

Desired result:

funcFile.go

imports ...

func returnTrue () bool {return true}

mapFile.go

func CreateFuncMap () {

    fns := make(map[string]func(bars []Types.MarketData) bool)
    otherFuncs := getFuncsFromFile("funcFile.go")
    for _, fn := range otherFuncs {
        fns[fn.Name] = fn
    }
}
JohnAllen
  • 7,317
  • 9
  • 41
  • 65
  • do you need a list of all functions or functions with a specific signature? – Ярослав Рахматуллин Jan 09 '23 at 11:48
  • they all have the same signature. – JohnAllen Jan 09 '23 at 11:58
  • Do you intend to run these functions in the program that is finding them? What isTypes.MarketData - is it significant? If not, then simplify your question of add a dummy struct for it in the question. Are you trying to load code dynamically at runtime? – Ярослав Рахматуллин Jan 09 '23 at 12:00
  • 1
    yes, trying to load code at runtime. To iterate through these functions and run them. The type is very significant. Though I will need to change these over time probably and may end up using generics. – JohnAllen Jan 09 '23 at 12:09
  • 1
    @JohnAllen what you're trying to do is simply not possible. What you can do however, is to explicitly specify the functions in the code. – mkopriva Jan 09 '23 at 13:11
  • yeah that's I've been doing but the list is long and it will get longer. I suspect there is a way to do this dynamically though. – JohnAllen Jan 09 '23 at 13:16
  • If I could just get the function body I could do it – JohnAllen Jan 09 '23 at 13:16
  • Sure seems like this should be possible in multiple senses of should – JohnAllen Jan 09 '23 at 13:19
  • 1
    Go is a compiled language. You should not access Go sources from Go code. After Go code is compiled, there are no sources anymore. Also, once the code is compiled, there is no separation into "files" anymore, there are only packages. Perhaps you can look into reflection? Or explain your actual problem. – rustyx Jan 09 '23 at 13:32
  • @rustyx My problem is that I have hundreds and eventually 1000s of individual functions I need to run and I don't want to have to write out each function name individually in a separate file or otherwise manually deal with them other then declaring them. – JohnAllen Jan 09 '23 at 13:38
  • @JohnAllen write a code generator if you don't want to write the map manually. That's a very doable task in Go. Trying to load functions at runtime is not so much. – mkopriva Jan 09 '23 at 13:39
  • 1
    That's a description of a solution for a problem. The problem begins way before you arrive at those 1000s functions. – rustyx Jan 09 '23 at 13:44
  • 2
    @JohnAllen, if it's all the same to you, you could redeclare those functions as methods, then all you need is an instance of the receiver and the `reflect` package. But with plain functions `reflect` will not help you. – mkopriva Jan 09 '23 at 13:44
  • Yeah I guess I will need to do something like this: https://stackoverflow.com/questions/21397653/how-to-dump-methods-of-structs-in-golang – JohnAllen Jan 09 '23 at 13:51

0 Answers0