import "github.com/robertkrimen/otto" func main() { vm := otto.New() result, _ := vm.Run("2 + 2") fmt.Println(result) // prints "4" }
import "github.com/robertkrimen/otto" func main() { vm := otto.New() script := ` function add(x, y) { return x + y; } ` vm.Run(script) result, _ := vm.Call("add", nil, 2, 3) fmt.Println(result) // prints "5" }
import "github.com/robertkrimen/otto" func main() { vm := otto.New() jsVal, _ := otto.ToValue(42) vm.Set("answer", jsVal) result, _ := vm.Run("answer * 2") fmt.Println(result) // prints "84" }Overall, the go github.com/robertkrimen/otto Otto package library provides a great way to incorporate JavaScript into Go programs.