Is there a eval() like method on golang?
Evaluate/Execute JavaScript code/expressions:
var x = 10;
var y = 20;
var a = eval("x * y") + "<br>";
var b = eval("2 + 2") + "<br>";
var c = eval("x + 17") + "<br>";
var res = a + b + c;
The result of res will be:
200
4
27
Is this possible in golang? and why?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With