func main() { ctx := gojs.NewContext() defer ctx.Release() s := gojs.NewString("Hello from go!") defer s.Release() fmt.Printf("%v %v\n", s.Length(), s.String()) fmt.Printf("%v %v\n", s.EqualToString("Hello"), s.EqualToString("Hello from go!")) obj := ctx.NewFunctionWithCallback(gojs.GoFunctionCallback(callback)) ctx.GlobalObject().SetProperty("f", obj.ToValue(), gojs.PropertyAttributeReadOnly) _, err := ctx.EvaluateScript("f()", nil, "", 1) if err != nil { panic(err) } ctx.EvaluateScript("var a = \"Go!\"", nil, "", 1) a, err := ctx.GlobalObject().GetProperty("a") fmt.Printf("%v %s %v\n", a, a.ToStringOrDie(), err) fmt.Printf("\nScripts...\n") print_result(ctx, "null") print_result(ctx, "false") print_result(ctx, "1234.123") print_result(ctx, "new Array(1, 2, 3)") print_result(ctx, "12+34\nreturn new 234 Array") print_result(ctx, "1/0") fmt.Printf("Done!\n") }
func main() { ctx := gojs.NewContext() defer ctx.Release() ret, err := ctx.EvaluateScript("['hello', 'world'].join(' ')", nil, ".", 0) if err != nil { fmt.Println("Script had an error :(", err) return } if ret == nil { fmt.Println("Nothing returned...") return } retstr := ret.ToStringOrDie() fmt.Println(retstr) }