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?
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.