如何让Golang执行一个字符串?

huangapple go评论98阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2015年2月28日 23:51:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/28783637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定