Package vert provides WebAssembly interop between Go and JS values.
GOOS=js GOARCH=wasm go get github.com/norunners/vertBelow is a trivial string value interop.
package main
import "github.com/norunners/vert"
func main() {
v := vert.ValueOf("Hello World!")
// Use v as a JS value.
s := ""
v.AssignTo(&s)
// Use s as a Go value.
}Go structs and JS objects interop seamlessly.
package main
import "github.com/norunners/vert"
type Data struct {
Message string
}
func main() {
v := vert.ValueOf(Data{
Message: "Hello World!",
})
// e.g. {"Message": "Hello World!"}
d := &Data{}
v.AssignTo(d)
}Tagged struct fields allow defined JS field names.
package main
import "github.com/norunners/vert"
type Data struct {
Message string `js:"msg"`
}
func main() {
v := vert.ValueOf(Data{
Message: "Hello World!",
})
// The defined JS tag names the field.
// e.g. {"msg": "Hello World!"}
d := &Data{}
v.AssignTo(d)
}AssignTo returns an error value.
package main
import "github.com/norunners/vert"
type Data struct {
Message string
}
func main() {
v := vert.ValueOf("Hello World!")
d := &Data{}
if err := v.AssignTo(d); err != nil {
// Handle error.
}
}Package syscall/js, of the Go standard library, has limited interop support between Go and JS values.
- The function
js.ValueOfwill not accept struct types. The result panics withValueOf: invalid value. - The type
js.Value.does not support anInterfaceor assignment method for non-basic Go values. However, the methodsBool,Int,FloatandStringsupport basic Go values.
Package vert leverages and extends syscall/js to accommodate these shortcomings.