A minimal setup example where TinyGo is compiled to a WASM executable runnable in the browser. We demonstrate a Go function export which can be called from JS, and a JS function export which can be called from Go.
The following instructions assume OSX 10.13+.
Install Golang and TinyGo if you don't already have them.
brew install go tinygo
Golang 1.13 is the minimum version needed to support TinyGo. Check go version
to make sure you have it.
Copy tinygo
's version of wasm_exec.js
into your project root.
This file must match the binary you use to build your WASM.
cp $(tinygo env TINYGOROOT)/targets/wasm_exec.js .
Install goexec
.
go mod init <your package name>
go get -u github.com/shurcooL/goexec
Compile project to WASM.
make build
Run a web server.
make serve
Note that we use goexec
to serve (see Makefile
) because you must use a
server that's aware of the WASM MIME type. A Python http.server
won't work.
Go to localhost:8080
in your browser. If all has gone well, you should see
"hello webassembly!" in the JS console.
If in doubt, triple-check that the version of wasm_exec.js
in your local
filesystem and loaded in your browser is as expected. A mismatched
wasm_exec.js
seemed to be the cause of most issues I ran into while setting
up.
- Lin Clark's illustrated intro to WASM
- Golang<>WASM quickstart
- Very helpful debug journal of a TinyGo<>WASM setup | Mariano Gappa
Don't want to run TinyGo locally? You can also grab a prebuilt docker
image
from Docker Hub.
docker run -v $GOPATH:/go -e "GOPATH=/go" tinygo/tinygo:0.18.0 tinygo build -o main.wasm