Go App可以动态编译Go代码吗?

Say I have a Lua program that accepts user input which happens to be valid Lua source code. This is sanitized, compiled and executed while the program is still running. Is (or will) such a thing (be) possible with Go?

I have an idea on how you could accomplish that, but you would pretty much have to do the same in C.

Go is a compiled language, so in order to achieve what you would like to accomplish you would need to write a wrapper over CSP that would support versioning of the binary and export functionality over some kind of a RPC. The steps would be as following:

  1. locally try to build the Go code
  2. start the result
  3. the new program connects to the RPC of the currently running program
  4. the first program is instructed to point all the CSP data (channel, goroutine scheduling) into the new runtime
  5. the external interface switches to the new program, after all goroutines from the old program end, kills the old process

Obviously this is ridiculously complicated and you'll end up saving a lot of time using a scripting language through something like Otto or go-lua.

I think that the following two projects have enough meat between them to help me achieve what I want. Neither is a perfect drop-in replacement but both can be extended to deliver a service that is close enough to what I originally did with dynamic compilation in Lua.

https://github.com/Knetic/govaluate

https://github.com/japm/goScript