Compile Go within Go?

683 views
Skip to first unread message

Dmitri Shuralyov

unread,
Oct 16, 2012, 3:02:24 AM10/16/12
to golan...@googlegroups.com
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.

Dan Kortschak

unread,
Oct 16, 2012, 3:24:51 AM10/16/12
to Dmitri Shuralyov, golan...@googlegroups.com
You're looking for something like this https://github.com/sbinet/go-eval, but you won't get compiled code.

--
 
 

Christoph Hack

unread,
Oct 16, 2012, 4:14:08 AM10/16/12
to golan...@googlegroups.com
One day Go will support loading dynamic libraries. Once that's the case, you could call the go compiler with something like the os/exec package, build your dynamic library and load it at runtime. But currently that's not supported, so you are probably better off by either using a project like go-eval, or by generating and building a separate stand-alone program and communicate with it using the rpc package.

Dmitri Shuralyov

unread,
Oct 16, 2012, 12:14:17 PM10/16/12
to golan...@googlegroups.com
Thank you for the ideas so far, they're very helpful!

I should update my original question to be a bit more general:

Intention: Execute the function described in goSourceCode string.
Input: goSourceCode string with the source code of the function (& provide standard input, perhaps?)
Output: standard output & standard error of the executed function?
Available Tools: no restrictions

What I want to do in the end is, of course, to execute the said function, not just compile it. Compiling it was just one idea of how I could go about executing it, but clearly there are other ways.

In the future, I'd want to make this function call asynchronous, i.e. "start executing the function in parallel ... and notify when it's done".

Given the above, I have another for a solution. Why not generate a .go source file with the said function, the packages it needs, all other functions it relies on, and a func main() { nameOfFunctionToBeExecuted() }, then start a process to perform "go build generatedGoSourceCode.go" and if that succeeds, follow it up with executing the generated "./generatedGoSourceCode" binary.

Dmitri Shuralyov

unread,
Oct 16, 2012, 12:17:55 PM10/16/12
to golan...@googlegroups.com
Basically, what Christoph Hack suggested.

Xing Xing

unread,
Oct 16, 2012, 10:10:20 PM10/16/12
to golan...@googlegroups.com
于 Tue, 16 Oct 2012 09:17:55 -0700 (PDT)
Dmitri Shuralyov <shur...@gmail.com> 写道:
Just like the nagios's plug-in:

Every plug-in is a executable file (binary or script)
Framework execute it for parsing the stdout/sdterr.

Right?

The only needed is a building system, similar with
http://play.golang.org.

Martin Bruse

unread,
Oct 17, 2012, 2:45:44 AM10/17/12
to golan...@googlegroups.com
I created https://github.com/zond/gosafe in a temporary state of insanity to do just that.

It compiles and runs Go code, and more.

Dmitri Shuralyov

unread,
Oct 17, 2012, 3:11:09 PM10/17/12
to golan...@googlegroups.com, doug....@gmail.com
It might also be interesting to know why you want it.  Self modifying code is basically never necessary unless you're optimizing for extremely high performance.  And then, you'd be better suited using assembly or inlined assembly.

Doug, I'm working on an experimental IDE that performs live compilation. See this thread: https://groups.google.com/forum/?fromgroups=#!starred/golang-nuts/2xxcasj1-0k Currently, it compiles the entire Go source code file and displays the results. But I'm working on making it let you execute individual functions.
Reply all
Reply to author
Forward
0 new messages