Go v1.21, scheduled for release in August 2023, adds experimental support for the WebAssembly System Interface (WASI). It’ll be possible to natively compile server-side WebAssembly applications with Go, making Go a first-class citizen along with C/C++, Rust and Zig.
GOOS=wasip1 GOARCH=wasm go build ...
WASI is still in preview and is missing some key features, e.g. the ability to create sockets. Some WebAssembly runtimes have taken the initiative to extend the WASI preview, enabling a wider range of applications to run as WebAssembly modules.
Today, we’re open sourcing our WASI library and sockets extension for the Wazero WebAssembly runtime (https://wazero.io): https://github.com/stealthrocket/wasi-go.
The WASI library includes a wasirun
command that can be used to run server-side WebAssembly applications compiled with Go:
~ $ cat << EOF > hello.go
package main
import "fmt"
func main() { fmt.Println("Hello, World!") }
EOF
~ $ GOOS=wasip1 GOARCH=wasm gotip build -o hello.wasm hello.go
~ $ wasirun hello.wasm
Hello, World!
Non-blocking I/O and basic networking is also supported when compiling apps from Go, e.g. see https://gist.github.com/chriso/6c71e968ef1002981a6ff46ceaa39ee3. Until Go v1.21 is released in August, you can use gotip (https://pkg.go.dev/golang.org/dl/gotip) to try GOOS=wasip1 features. The provided sockets extension is compatible with the WasmEdge (https://wasmedge.org) runtime, allowing wasirun to run more complicated server-side applications compiled from C/C++/Rust/Zig for WasmEdge.