GJS provides a comprehensive API to access the WebAssembly host environment when using the js/wasm architecture. Its API is based on JavaScript semantics and provides WebAssembly interop between Go and JS values. Its current scope is to provide a comprehensive and well-tested API.
GOOS=js GOARCH=wasm go get github.com/chumaumenze/gjs
package main
import (
"github.com/chumaumenze/gjs"
)
type Data struct {
Code int
Message string `json:"message"`
inner any
}
func main() {
data := Data{
Code: 200,
Message: "Hello World!",
inner: "I am ignored",
}
_ = gjs.ValueOf(data) // e.g. {Code: 200, "message": "Hello World!"}
}
The Go standard library's syscall/js
package offers limited compatibility between Go and JavaScript values.
- The
js.Value
type lacks support for interfaces or assignment methods for complex Go values. - The
js.ValueOf
function cannot handle complex types likestruct
s, leading to a panic with the error messageValueOf: invalid value
.
To overcome these limitations, gjs builds upon and extends the functionality of syscall/js.