评估/执行Golang代码/表达式,例如js的eval() [英] Evaluate/Execute Golang code/expressions like js' eval()

查看:951
本文介绍了评估/执行Golang代码/表达式,例如js的eval()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

golang上是否有类似eval()的方法?

评估/执行JavaScript代码/表达式:

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;

res的结果将是:

200
4
27

在golang中这可能吗?为什么?

Is this possible in golang? and why?

推荐答案

在golang中这可能吗?为什么?

Is this possible in golang? and why?

不,因为golang不是那种语言.它旨在进行编译而不是进行解释,以便运行时不包含任何字符串到代码"转换器,或者实际上知道语法正确的程序是什么样子.

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.

请注意,与大多数其他编程语言一样,在Go中,您可以编写自己的解释器,即,该函数采用字符串并导致相应地执行计算. Go设计师的选择不仅仅是要对不需要的每个人都赋予这种可疑的兴趣和安全性.

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.

这篇关于评估/执行Golang代码/表达式,例如js的eval()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆