1. 第三方库 otto git地址
GitHub - robertkrimen/otto: A JavaScript interpreter in Go (golang)A JavaScript interpreter in Go (golang). Contribute to robertkrimen/otto development by creating an account on GitHub.
https://github.com/robertkrimen/otto
2. 使用 示例:
package main
import (
"fmt"
"github.com/robertkrimen/otto"
"log"
)
func main() {
vm := otto.New()
//传入golang变量
_ = vm.Set("param", "abc123")
v, err := vm.Run(`
function A(){
this.a=1;
this.add= function(){
this.a++;
return this;
}
};
var a = new A();
a.add().add();
a.a;
`)
if err != nil {
log.Fatal(err)
}
//输出执行结果。
fmt.Println(v)
}
// 输出 3
3. 作为 golang的扩展脚本 是很好的选择。(不支持 Es6的语法)