Created
February 12, 2016 22:39
-
-
Save jig/bc50ad2d3402ff3efb09 to your computer and use it in GitHub Desktop.
Go + Otto minimal sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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