英文:
how to make golang execute a string
问题
我不是要求在当前上下文中让golang执行某种形式的"eval",只是需要它接受一个输入字符串(比如从网络接收到的)并将其作为一个独立的golang程序执行。
理论上,当你运行go run testme.go
时,它会将testme.go
的内容读入一个字符串中,并对其进行解析、编译和执行。想知道是否可能调用一个go函数来直接执行一个字符串。请注意,我有一个要求,不要将字符串写入文件。
更新1
我真的想知道是否有一个函数(在某个go包中)作为go的入口点,换句话说,我可以用一个字符串作为参数调用这个函数,它会像go run testme.go
一样运行,其中testme.go包含了该字符串的内容。
英文:
I am not asking to make golang do some sort of "eval" in the current context, just need it to take a input string (say, received from network) and execute it as a separate golang program.
In theory, when you run go run testme.go
, it will read the content of testme.go
into a string and parse, compile and execute it. Wonder if it is possible to call a go function to execute a string directly. Note that I have a requirement not to write the string into a file.
UPDATE1
I really want to find out if there is a function (in some go package) that serves as an entry point of go, in another word, I can call this function with a string as parameter, it will behave like go run testme.go
, where testme.go has the content of that string.
答案1
得分: 4
据我所知,这是不可能的,Go编译器必须写入中间文件,所以即使你使用go run
而不是go build
,为了运行代码而创建文件,只是在必要时进行清理。因此,即使你设法让编译器不从文件中获取源代码,也无法在不接触磁盘的情况下运行Go程序。
例如,对一个简单的Hello World程序调用go run
并使用strace
进行跟踪,会显示如下行:
mkdir("/tmp/go-build167589894", 0700)
// ....
mkdir("/tmp/go-build167589894/command-line-arguments/_obj/exe/", 0777)
// ... 最后
unlink("/tmp/go-build167589894/command-line-arguments/_obj/exe/foo")
// ^^^ 我的程序名为foo.go
// ....
// 最后:
rmdir("/tmp/go-build167589894")
因此,你可以看到go run
在幕后进行了大量的磁盘写入操作,只是在之后进行清理。
我想,如果你愿意的话,可以挂载一些tmpfs并在其中构建,但除此之外,我不认为这是可能的。
英文:
AFAIK it cannot be done, and the go compiler has to write intermediate files, so even if you use go run
and not go build
, files are created for the sake of running the code, they are just cleaned up if necessary. So you can't run a go program without touching the disk, even if you manage to somehow make the compiler take the source not from a file.
For example, running strace
on calling go run
on a simple hello world program, shows among other things, the following lines:
mkdir("/tmp/go-build167589894", 0700)
// ....
mkdir("/tmp/go-build167589894/command-line-arguments/_obj/exe/", 0777)
// ... and at the end of things
unlink("/tmp/go-build167589894/command-line-arguments/_obj/exe/foo")
// ^^^ my program was called foo.go
// ....
// and eventually:
rmdir("/tmp/go-build167589894")
So you can see that go run does a lot of disk writing behind the scenes, just cleans up afterwards.
I suppose you can mount some tmpfs and build in it if you wish, but otherwise I don't believe it's possible.
答案2
得分: 2
我知道这个问题已经有5年了,但我想说现在是可能的,对于任何寻找最新答案的人来说。
Golang编译器本身就是用Go语言编写的,理论上可以嵌入到Go程序中。不过这可能会相当复杂。
作为一个更好的选择,有一些项目(比如yaegi)可以将Go解释器嵌入到Go程序中。
英文:
I know that this question is (5 years) old but I wanted to say that actually, it is possible now, for anyone looking for an up-to-date answer.
The Golang compiler is itself written in Go so can theoretically be embedded in a Go program. This would be quite complicated though.
As a better alternative, there are projects like yaegi which are effectively a Go interpreter which can be embedded into Go programs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论