Using WASI

How to use TinyGo with the WebAssembly System Interface (WASI).

TinyGo is very useful for compiling programs for use on servers and other edge devices (WASI).

TinyGo programs can run in Extism, Fastly Compute@Edge, Fermyon Spin, tau, wazero and many other WebAssembly runtimes.

Both WASI Preview 1 (wasip1) and WASI Preview 2 (wasip2) are currently supported.

Here is a small TinyGo program for use within a WASI host application:

package main

//go:wasmimport yourmodulename add
func add(x, y uint32) uint32

// main is required for the `wasip1` target, even if it isn't used.
func main() {}

//go:wasmimport may only appear on function declarations without a body (the implementation is provided by the host).

To compile the above TinyGo program for use on any WASI runtime:

GOOS=wasip1 GOARCH=wasm tinygo build -o main.wasm main.go