go-ejs is a wrapper of goja. go-ejs is intended to embed js in golang applications easily.
The package is fully go-getable, So, just type
go get github.com/rosbit/go-ejs
to install.
package main
import "fmt"
import "github.com/rosbit/go-ejs"
func main() {
ctx := ejs.NewContext()
res, _ := ctx.Eval("1 + 2")
fmt.Println("result is:", res)
}
Suppose there's a JavaScript file named a.js
like this:
function add(a, b) {
return a+b
}
one can call the JavaScript function add() in Go code like the following:
package main
import "fmt"
import "github.com/rosbit/go-ejs"
var add func(int, int)int
func main() {
ctx := ejs.NewContext()
if err := ctx.LoadFile("a.js", nil); err != nil {
fmt.Printf("%v\n", err)
return
}
if err := ctx.BindFunc("add", &add); err != nil {
fmt.Printf("%v\n", err)
return
}
res := add(1, 2)
fmt.Println("result is:", res)
}
JavaScript calling Go function is also easy. Just bind a golang func with a var name
with AddVar("funcname", function)
. There's the example:
package main
import "github.com/rosbit/go-ejs"
// function to be called by JavaScript
func adder(a1 float64, a2 float64) float64 {
return a1 + a2
}
func main() {
ctx := ejs.NewContext()
ctx.AddVar("adder", adder)
ctx.EvalFile("b.js", nil) // b.js containing code calling "adder"
}
In JavaScript code, one can call the registered name directly. There's the example b.js
.
r = adder(1, 100) // the function "adder" is implemented in Go
console.log(r)
package main
import "github.com/rosbit/go-ejs"
import "fmt"
func adder(a1 float64, a2 float64) float64 {
return a1 + a2
}
func main() {
vars := map[string]interface{}{
"adder": adder, // to JavaScript built-in function
"a": []int{1,2,3}, // to JavaScript array
}
ctx := js.NewContext()
if err := ctx.LoadFile("file.js", vars); err != nil {
fmt.Printf("%v\n", err)
return
}
// or call ctx.AddVars(vars) to add variables.
res, err := ctx.GetGlobals("global_var_name") // get the value of var global_var_name
if err != nil {
fmt.Printf("%v\n", err)
return
}
fmt.Printf("res:", res)
}
The package is not fully tested, so be careful.
Pull requests are welcome! Also, if you want to discuss something send a pull request with proposal and changes. Convention: fork the repository and make changes on your fork in a feature branch.