https://github.com/oneofone/wjsu
syscall/js wasm helper
https://github.com/oneofone/wjsu
go golang wasm webassembly
Last synced: 2 months ago
JSON representation
syscall/js wasm helper
- Host: GitHub
- URL: https://github.com/oneofone/wjsu
- Owner: OneOfOne
- License: apache-2.0
- Created: 2018-12-31T03:06:12.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-07-25T01:53:06.000Z (almost 5 years ago)
- Last Synced: 2025-01-21T05:25:01.735Z (over 1 year ago)
- Topics: go, golang, wasm, webassembly
- Language: Go
- Homepage:
- Size: 34.2 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# wjsu [](https://godoc.org/github.com/OneOfOne/wjsu)
wjsu (aka Webassembly Javascript Utils) is a helper package for [`syscall/js`](https://godoc.org/syscall/js).
Inpsired by [honnef.co/go/js/dom](https://github.com/dominikh/go-js-dom).
## Install
GOOS=js GOARCH=wasm go get -u github.com/OneOfOne/wjsu
* **note**: the package requires Go 1.12 for the `syscall/js` changes.
## Features
* Object.
* Array (partial).
* console (Log, Warn and Error).
* wrapper for loading external scripts.
* wrapper for js.ValueOf that doesn't panic and handles slices / maps.
## Usage
x
### main.go
```go
//+build js,wasm
package main
import (
"time"
"github.com/OneOfOne/wjsu"
)
func main() {
if err := wjsu.Initialize(); err != nil {
panic(err)
}
wjsu.Console.Log("hello console", []int{1, -1}, map[string]float64{"a": 1, "z": -1})
wjsu.Document.QuerySelector("div#loader").SetInnerHTML("hello world")
app := wjsu.Document.QuerySelector("app")
for t := range time.Tick(time.Second) {
app.SetInnerHTML("time (UTC): " + t.UTC().String())
}
}
```
### index.html
```html
WASM Loader
const goInit = () => {
const go = new Go();
WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then((result) => {
go.run(result.instance);
});
}
Loading...
```
### serve.go
```go
//+build !js
package main
import (
"net/http"
"os/exec"
"time"
"log"
)
func main() {
time.AfterFunc(time.Second/2, func() {
exec.Command("xdg-open", "http://localhost:8080").Start()
})
log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("."))))
}
```
### build & run
```sh
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" .
GOOS=js GOARCH=wasm go build -o main.wasm
go run serve.go
```
#### Tinygo
```sh
$ pacman -S tinygo
./tiny.sh path/to/project
```
## VSCode settings
```json
{
"go.toolsEnvVars": {
"GOOS": "js",
"GOARCH": "wasm"
}
}
```
## TODO
* Proper documentation.
## License
This project is released under the Apache v2. licence. See [LICENCE](LICENCE) for more details.