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

后端 未结 5 2151
再見小時候
再見小時候 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:59

    Its perfectly possible. At least for expressions, which seems to be what you want:

    Have a look at:

    • https://golang.org/src/go/types/eval.go

    • https://golang.org/src/go/constant/value.go

    • https://golang.org/pkg/go/types/#Scope

    You'd need to create your own Package and Scope objects and Insert constants to the package's scope. Constants are created using types.NewConst by providing appropriate type information.

提交回复
热议问题