英文:
Can a Go App dynamically compile Go code?
问题
我有一个接受用户输入的 Lua 程序,该输入恰好是有效的 Lua 源代码。在程序运行时,这些输入会经过清理、编译和执行。在 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?
答案1
得分: 2
我认为以下两个项目之间有足够的内容可以帮助我实现我想要的目标。虽然它们都不是完美的即插即用替代品,但都可以进行扩展,以提供与我最初在Lua中使用动态编译所做的工作相似的服务。
https://github.com/Knetic/govaluate
https://github.com/japm/goScript
英文:
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.
答案2
得分: 1
我有一个关于如何实现这个的想法,但你基本上需要在C语言中做同样的事情。
Go是一种编译语言,所以为了实现你想要的功能,你需要编写一个包装器来支持二进制文件的版本控制和某种RPC的导出功能。具体步骤如下:
- 本地尝试构建Go代码。
- 启动结果。
- 新程序连接到当前运行程序的RPC。
- 第一个程序被指示将所有CSP数据(通道、goroutine调度)指向新的运行时。
- 外部接口切换到新程序,在旧程序的所有goroutine结束后,终止旧进程。
显然,这非常复杂,使用类似于Otto或go-lua的脚本语言会节省很多时间。
英文:
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:
- locally try to build the Go code
- start the result
- the new program connects to the RPC of the currently running program
- the first program is instructed to point all the CSP data (channel, goroutine scheduling) into the new runtime
- 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
得分: 0
Traefik反向代理背后的团队维护着yaegi,这是一个用于Go语言的解释器。
英文:
The team behind the Traefik reverse proxy maintains yaegi, an interpreter for Go.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论