Skip to content

Instantly share code, notes, and snippets.

@jig
Created February 12, 2016 22:39
Show Gist options
  • Save jig/bc50ad2d3402ff3efb09 to your computer and use it in GitHub Desktop.
Save jig/bc50ad2d3402ff3efb09 to your computer and use it in GitHub Desktop.
Go + Otto minimal sample
package main
import (
"fmt"
"github.com/robertkrimen/otto"
)
func main() {
vm := otto.New()
vm.Run(`
abc = 2 + 2;
console.log("The value of abc is " + abc); // 4
`)
if value, err := vm.Get("abc"); err == nil {
if value_int, err := value.ToInteger(); err == nil {
fmt.Printf("%d, %#v\n", value_int, err)
}
}
vm.Set("twoPlus", func(call otto.FunctionCall) otto.Value {
right, _ := call.Argument(0).ToInteger()
result, _ := vm.ToValue(2 + right)
return result
})
result, _ := vm.Run(`
result = twoPlus(2.0); // 4
`)
fmt.Printf("%#v\n", result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment