import ( "github.com/robertkrimen/otto" "fmt" ) func main() { vm := otto.New() result, err := vm.Eval("1 + 2") if err != nil { fmt.Println("Error:", err) } else { fmt.Println("Result:", result) } }
Result: 3
import ( "github.com/robertkrimen/otto" "fmt" ) func main() { vm := otto.New() vm.Set("hello", func(call otto.FunctionCall) otto.Value { fmt.Println("Hello, world!") return otto.Value{} }) vm.Run(` hello(); `) }
Hello, world!Overall, the Otto package provides a powerful and flexible way to integrate JavaScript code into a Go application. It is a great library for adding scripting capabilities to your application.