WebAssembly and wasip1
WebAssembly and wasip1
Overview
Go can target js/wasm (browser + wasm_exec.js) and wasip1 (WASI preview1) for server-side Wasm runtimes. Constraints differ sharply from linux/amd64.
Diagram: Wasm targets
Go source
├── GOOS=js GOARCH=wasm ──► browser + wasm_exec
└── GOOS=wasip1 GOARCH=wasm ──► WASI runtime
Targets
GOOS=js GOARCH=wasm go build -o main.wasm .
GOOS=wasip1 GOARCH=wasm go build -o main.wasm .Constraints
| Area | Reality |
|---|---|
| Threads | Limited / host-dependent |
| Networking | WASI and browser models differ |
| Filesystem | Capability-based under WASI |
| cgo | Generally unavailable |
| Binary size | Larger than tiny Go-less Wasm; use strip/trimpath |
Use Cases
- Policy plugins
- Edge filters
- Portable CLI sandboxes
- Browser tools (js/wasm)
Experiment
# if wasip1 supported:
printf 'package main\nimport "fmt"\nfunc main(){ fmt.Println("hi wasm") }\n' > /tmp/w.go
cd /tmp && GOOS=wasip1 GOARCH=wasm go build -o hi.wasm w.go
ls -la hi.wasm
# run with a WASI runtime if installed, e.g. wasmtime hi.wasmWhat to notice: Build succeeds without a local WASI runner; size is non-trivial compared to a hello C program.
Try next: Compare GOOS=wasip1 vs native binary size with -ldflags='-s -w'.