1

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?

7
  • 2
    Is: No. Will be: Probably no. Commented Mar 30, 2017 at 13:02
  • darrencoxall.com/golang/executing-commands-in-go You could always just manually save input to a file, then run a compiler from os/exec Commented Mar 30, 2017 at 13:26
  • Does it mean that Go lacks true reflection? Commented Mar 30, 2017 at 13:46
  • 1
    @EgorSkriptunoff: Compiling and executing new code at runtime has nothing to do with reflection. On linux you could also compile and load the code as a plugin, but this still isn't really the same as what a interpreted language like lua can do. Commented Mar 30, 2017 at 14:14
  • @JimB: strictly speaking Lua does compile code (to a VM); LuaJIT compiles to a VM and then jit-compiles this to machine code. What I like about Go is that UTF-8 is everywhere and also the goroutine/channel stuff but alas, there are also things that will just not translate (easily). The execution speed for code created by Go is not always comparable to LuaJIT though this may partly have to do with the fact that I'm not yet doing things in an optimal Go way. Perhaps adding the LuaJIT core as a package to Go (so Lua code can be executed on the fly) is the way forward here. Commented Mar 30, 2017 at 15:03

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

AKA, you build a mini distributed system with a main RPC controller that connects to the part you want to be able to rewrite.
I agree that this is probably not worth the effort and that something like go-lua or golua (github.com/aarzilli/golua) may be a more sensible approach.
This is the kind of thing I would randomly do in my spare time, just to see if it works. Thanks for the idea, I will have some fun with this @Piotr Zduniak
0

The team behind the Traefik reverse proxy maintains yaegi, an interpreter for Go.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.