Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/norunners/vert
WebAssembly interop between Go and JS values.
https://github.com/norunners/vert
go golang interop webassembly
Last synced: 3 months ago
JSON representation
WebAssembly interop between Go and JS values.
- Host: GitHub
- URL: https://github.com/norunners/vert
- Owner: norunners
- License: mit
- Created: 2018-03-25T17:26:47.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2022-12-03T07:58:38.000Z (almost 2 years ago)
- Last Synced: 2024-06-18T16:59:39.864Z (5 months ago)
- Topics: go, golang, interop, webassembly
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 98
- Watchers: 7
- Forks: 15
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-extra - vert - 03-25T17:26:47Z|2021-12-29T04:52:53Z| (WebAssembly / Routers)
README
# vert
[![Go Reference](https://pkg.go.dev/badge/github.com/norunners/vert.svg)](https://pkg.go.dev/github.com/norunners/vert)
[![Test](https://github.com/norunners/vert/actions/workflows/test.yml/badge.svg)](https://github.com/norunners/vert/actions/workflows/test.yml)Package `vert` provides WebAssembly interop between Go and JS values.
## Install
```bash
GOOS=js GOARCH=wasm go get github.com/norunners/vert
```## Examples
### Hello World!
Below is a trivial string value interop.
```go
package mainimport "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.
}
```### Structs & Objects
Go structs and JS objects interop seamlessly.
```go
package mainimport "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
Tagged struct fields allow defined JS field names.
```go
package mainimport "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)
}
```## Error Handling
`AssignTo` returns an `error` value.
```go
package mainimport "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.
}
}
```## Why?
Package `syscall/js`, of the Go standard library, has limited interop support between Go and JS values.
1. The function `js.ValueOf` will not accept struct types. The result panics with `ValueOf: invalid value`.
2. The type `js.Value.` does not support an `Interface` or *assignment* method for non-basic Go values.
However, the methods `Bool`, `Int`, `Float` and `String` support basic Go values.Package `vert` leverages and extends `syscall/js` to accommodate these shortcomings.
## License
* [MIT License](LICENSE)