Skip to content

Commit 7130bd4

Browse files
docs: add native Go (Go 1.21) WASI documentation
Signed-off-by: Aaradhy Chinche <aaradhychinche@gmail.com> git push --force-with-lease
1 parent 2d3444d commit 7130bd4

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

docs/develop/go/hello_world.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ sidebar_position: 1
44

55
# TinyGo
66

7+
This page focuses on using TinyGo with WasmEdge. Native Go (Go 1.21+) support is documented separately.
8+
79
The best way to run Go programs in WasmEdge is to compile Go source code to WebAssembly using [TinyGo](https://tinygo.org/). In this article, we will show you how.
810

9-
The Golang is adding WASI support. Stay tuned!
11+
Native Go (Go 1.21+) now supports compiling to WASI. For details on using native Go with WasmEdge, see the Native Go WASI guide.
12+
1013

1114
## Install TinyGo
1215

docs/develop/go/native-go-wasi.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
5+
# Native Go with WASI (Go 1.21+)
6+
7+
Starting with Go 1.21, the Go toolchain natively supports compiling applications to the WebAssembly System Interface (WASI). This enables developers to build WebAssembly applications using the standard Go compiler and run them on WASI-compliant runtimes such as WasmEdge.
8+
9+
This page describes how to develop and run WASI-based applications using native Go with WasmEdge. It also highlights key differences compared to using TinyGo, which has historically been the primary way to run Go applications on WebAssembly.
10+
11+
Native Go support is particularly useful for developers who require functionality that is not fully available in the TinyGo standard library, such as more complete language features or integration with existing Go codebases.
12+
13+
14+
## TinyGo vs Native Go
15+
16+
There are currently two ways to develop Go applications for WasmEdge: using TinyGo or using the native Go compiler with WASI support introduced in Go 1.21. Each approach has different trade-offs.
17+
18+
| Aspect | TinyGo | Native Go (Go 1.21+) |
19+
|------|--------|----------------------|
20+
| Compiler | TinyGo (LLVM-based) | Standard Go compiler |
21+
| Standard library | Partial | More complete |
22+
| Binary size | Smaller | Larger |
23+
| WASI support | Mature | Introduced in Go 1.21 |
24+
| Networking | Not available by default | Not available by default (WASI limitation) |
25+
| Typical use cases | Small, resource-constrained workloads | Existing Go codebases, richer language features |
26+
27+
TinyGo has historically been the primary way to run Go applications on WebAssembly and remains a good choice for lightweight workloads. Native Go with WASI support enables developers to reuse more existing Go code and tooling, but it is still subject to the limitations of the WASI environment.
28+
29+
30+
## Building a WASI module with Go 1.21
31+
32+
To compile a Go application to a WASI-compatible WebAssembly module, Go 1.21 or newer is required.
33+
34+
Use the following environment variables when building your application:
35+
36+
```bash
37+
GOOS=wasip1 GOARCH=wasm go build -o app.wasm
38+
```
39+
40+
In this command:
41+
42+
GOOS=wasip1 specifies the WASI Preview 1 target
43+
44+
GOARCH=wasm selects the WebAssembly architecture
45+
46+
app.wasm is the generated WebAssembly module
47+
48+
The resulting .wasm file can be executed by WASI-compliant runtimes such as WasmEdge.
49+
50+
51+
## Running the module with WasmEdge
52+
53+
After building the WebAssembly module (for example, `app.wasm`), it can be executed using the WasmEdge CLI.
54+
55+
```bash
56+
wasmedge app.wasm
57+
```
58+
59+
WasmEdge executes the WASI _start entry point by default. Command-line arguments provided after the module path are forwarded to the module at runtime. The --dir option can be used to preopen host directories for file system access when required.
60+
61+
62+
## Networking and WASI limitations
63+
64+
The current WASI Preview 1 specification does not include native support for networking primitives such as TCP or UDP sockets. As a result, Go packages that depend on the standard `net` or `net/http` libraries cannot function out of the box in a WASI environment.
65+
66+
This limitation applies to both TinyGo and native Go when targeting WASI and is not specific to WasmEdge. Networking support requires additional abstractions or host-provided functionality, which are discussed in the following sections.
67+
68+
69+
## Networking with stealthrocket/net
70+
71+
To work around the lack of native networking support in WASI Preview 1, alternative approaches are required. One such approach is the use of the `stealthrocket/net` project, which provides a networking implementation designed for WASI environments.
72+
73+
Rather than relying on traditional socket APIs, `stealthrocket/net` enables networking by forwarding requests to host-provided functionality. This allows Go applications compiled to WASI to perform HTTP requests and other network operations when supported by the runtime environment.
74+
75+
When using native Go with WASI, libraries such as `stealthrocket/net` can be used to enable networking functionality that is otherwise unavailable through the standard Go `net` and `net/http` packages.
76+
77+
78+
## Importing host functions in Go 1.21
79+
80+
In addition to using libraries such as `stealthrocket/net`, native Go applications compiled to WASI can interact with runtime-provided functionality by importing host functions.
81+
82+
Starting with Go 1.21, host functions can be declared using the `//go:wasmimport` directive. This allows Go code to call functions that are implemented by the WebAssembly runtime, such as WasmEdge, rather than inside the WebAssembly module itself.
83+
84+
Host functions can be used to expose capabilities that are not available in the WASI specification, including networking, system integration, or custom platform services. The exact set of available host functions depends on the runtime and its configuration.

0 commit comments

Comments
 (0)