https://github.com/rosbit/go-ejs
embding js in golang is so easy.
https://github.com/rosbit/go-ejs
embeddable goja golang js
Last synced: 30 days ago
JSON representation
embding js in golang is so easy.
- Host: GitHub
- URL: https://github.com/rosbit/go-ejs
- Owner: rosbit
- License: mit
- Created: 2021-12-16T01:50:01.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-04-03T02:10:09.000Z (about 2 years ago)
- Last Synced: 2025-05-08T22:37:14.511Z (11 months ago)
- Topics: embeddable, goja, golang, js
- Language: Go
- Homepage:
- Size: 4.65 MB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Embeding JS for golang
go-ejs is a wrapper of [goja](https://github.com/dop251/goja). go-ejs is intended to
embed js in golang applications easily.
### Usage
The package is fully go-getable, So, just type
`go get github.com/rosbit/go-ejs`
to install.
```go
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)
}
```
### Go calls JavaScript function
Suppose there's a JavaScript file named `a.js` like this:
```javascript
function add(a, b) {
return a+b
}
```
one can call the JavaScript function add() in Go code like the following:
```go
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 calls Go function
JavaScript calling Go function is also easy. Just bind a golang func with a var name
with `AddVar("funcname", function)`. There's the example:
```go
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`.
```javascript
r = adder(1, 100) // the function "adder" is implemented in Go
console.log(r)
```
### add more than 1 variables and functions at one time
```go
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)
}
```
### Status
The package is not fully tested, so be careful.
### Contribution
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.