This is a page for tools for go running in wasm
To build the wasm file run:
GOOS=js GOARCH=wasm go build -o main.wasm
In order use wasm in Go you need Go's wasm_exec.js
file.
This repo is uses jsDeliver as a CDN from the Go repo to get that file with this HTML header:
<script src="https://cdn.jsdelivr.net/gh/golang/[email protected]/misc/wasm/wasm_exec.js"></script>
To copy the file from your computer's Go install to your current directory use this command:
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" .
The way to expose Go functions to the Javascript is by importing syscall/js
and calling js.Global().Set
with a js.FuncOf
wrapper around your Go function. The name given in .Set
will be what is available in the Javascript.
Go by default ends execution as soon as the main function completes. If you want the functions to be available you will have to keep the main function alive. I do this by having a channel at the end of main
:
<-make(chan bool)