I'd like to have access to the following functionality.
func CompileGoFunc(goSourceCode string) func()
(to simplify this for now, assume the compiled function takes no parameters and doesn't return anything)
Intention: Compile the function described in goSourceCode string and return the result function.
Input: goSourceCode string, with the source code of the function
Output: the compiled function
Available Tools: anything
Here is a sample implementation that works for only one function. Obviously I want a general solution, but I'm providing this to give a better idea of what I want.
package main
import (
"fmt"
)
func sample() { fmt.Println("Sample!"); }
func CompileGoFunc(goSourceCode string) func() {
if "func sample() { fmt.Println(\"Sample!\"); }" == goSourceCode {
return sample;
}
return nil;
}
func main() {
f := CompileGoFunc("func sample() { fmt.Println(\"Sample!\"); }")
f()
}
Does anyone have any ideas about how this might be done?
Is there a way in Go to generate dynamic libraries? If so, you could use the "go" binary to generate such a library, dynamically load it and provide access to the function, or something.
Since the compiler for Go is open source, it might be possible to tap into its functionality directly. Of course, this would be a lot harder and require more maintenance.
Anyway, I'm just brainstorming. Please offer any solutions you can think of.
---
P.S. Go is self-described as "a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language." This task would be trivial with an interpreted language, but I hope it's also feasible to achieve in Go.