Evaluate/Execute Golang code/expressions like js' eval()

后端 未结 5 2152
再見小時候
再見小時候 2021-01-02 12:02

Is there a eval() like method on golang?

Evaluate/Execute JavaScript code/expressions:

var x = 10;
var y = 20;
var a = eval(\"x * y\         


        
5条回答
  •  既然无缘
    2021-01-02 12:57

    Is this possible in golang? and why?

    No, because golang is not that kind of language. It is intended to be compiled, not interpreted, so that the runtime does not contain any “string to code” transformer, or indeed knows what a syntactically correct program looks like.

    Note that in Go as in most other programming languages, you can write your own interpreter, that is, a function that takes a string and causes computations to be done accordingly. The choice of the Go designers is only not to force a feature of such dubious interest and security on everyone who did not need it.

提交回复
热议问题