Created
September 23, 2013 20:48
-
-
Save crosbymichael/6676738 to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"flag" | |
"fmt" | |
"github.com/robertkrimen/otto" | |
) | |
type Person struct { | |
Name string | |
Age int | |
} | |
func main() { | |
flag.Parse() | |
people := []Person{{"Koye", 3}, {"Sam", 24}} | |
runtime := otto.New() | |
// Allow a go func to be called from js | |
runtime.Set("dostuff", func(c otto.FunctionCall) otto.Value { | |
fmt.Println(c.ArgumentList) | |
return otto.UndefinedValue() | |
}) | |
if _, err := runtime.Run("dostuff(1,2,3);"); err != nil { | |
panic(err) | |
} | |
fnc := `function printValues(v) { | |
for (var i =0; i < v.length; i++) { | |
console.log(v[i].Name); | |
} | |
}` | |
if _, err := runtime.Run(fnc); err != nil { | |
panic(err) | |
} | |
v, err := runtime.ToValue(people) | |
if err != nil { | |
panic(err) | |
} | |
// Pass go values to a js function | |
if _, err := runtime.Call("printValues", nil, v); err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment