WebAssembly, WASI and alternative Go implementations
WebAssembly, WASI and alternative Go implementations
WebAssembly (Wasm) is a portable binary instruction format executed by a host runtime. Go supports GOOS=js GOARCH=wasm for a JavaScript host with the matching wasm_exec.js support code; GOOS=wasip1 GOARCH=wasm targets WASI Preview 1, an interface for host services such as standard streams. WASI support arrived in Go 1.21. A Wasm module still depends on compatible host imports and granted capabilities. A pure calculation can be tested natively first.
package main
import ( "fmt")
func sum(xs []int) int { n := 0 for _, x := range xs { n += x } return n}func main() { fmt.Println(sum([]int{2, 3, 5}))}Save as main.go. Build separately with CGO_ENABLED=0 GOOS=wasip1 GOARCH=wasm go build -o wasi.wasm main.go and CGO_ENABLED=0 GOOS=js GOARCH=wasm go build -o js.wasm main.go. Run the WASI command in a compatible runtime with minimal granted capabilities; run the JS target through the matching Go JavaScript shim. Their binary format is shared, but their host interfaces are not interchangeable. On the tested Go 1.23.2 installation and Node 22, run the JS example with GOMAXPROCS=1 node "$(go env GOROOT)/misc/wasm/wasm_exec_node.js" js.wasm; an inherited GOMAXPROCS=2 failed because this port cannot create OS threads. Use the path and runtime settings documented by your own Go version. For WASI use the provided fixtures/native-host/wasi-host.cjs with Node 22; the lab grants no preopened directories. The Node WASI implementation is experimental and this is not browser testing. Go 1.24 introduced wasmexport and WASI reactor support; those newer features are outside the installed Go 1.23.2 lab.
Expected output:
10Check the format header, not host compatibility
package main
import ( "bytes" "fmt")
func wasmHeader(b []byte) bool { return len(b) >= 8 && bytes.Equal(b[:8], []byte{0, 97, 115, 109, 1, 0, 0, 0})}func main() { fmt.Println(wasmHeader([]byte{0, 97, 115, 109, 1, 0, 0, 0})) fmt.Println(wasmHeader([]byte{0, 97}))}This only checks the magic bytes and core format version, not module validity, imports or safety. A real host validates the complete module. TinyGo is an alternative compiler with embedded and WebAssembly targets; check its target-specific language and standard-library support, scheduler and GC settings before assuming equivalence to the standard Go compiler. No TinyGo toolchain is installed in this lab. Choose the compiler based on measured size, required APIs and target execution tests, not a blanket claim that one is faster.
Expected output:
truefalseTry it locally
Write a pure function counting bytes equal to a specified value, and test with nonempty and empty input. Build the same source for WASI Preview 1; keep host execution evidence separate from the native test.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Native interoperability 07: WebAssembly, WASI and alternative Go implementations
Check the lesson objective before moving on.
Why is a JS-target Wasm module not necessarily runnable by a WASI host?
What does a successful native Go test prove about TinyGo?