Skip to content

Commit 365877d

Browse files
docs: add native Go (Go 1.21) WASI documentation (#284)
Signed-off-by: Aaradhy Chinche <aaradhychinche@gmail.com>
1 parent 2d3444d commit 365877d

File tree

2 files changed

+164
-1
lines changed

2 files changed

+164
-1
lines changed

docs/develop/go/hello_world.md

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

55
# TinyGo
66

7+
This page focuses on using TinyGo with WasmEdge.
8+
Native Go (Go 1.21+) support is documented separately in
9+
[native Go WASI documentation](./native-go-wasi.md).
10+
11+
712
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.
813

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

1117
## Install TinyGo
1218

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

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
| WASI support | Mature | Introduced in Go 1.21 |
23+
| Networking | Not available by default | Not available by default (WASI limitation) |
24+
| Typical use cases | Small, resource-constrained workloads | Existing Go codebases, richer language features |
25+
26+
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.
27+
28+
29+
## Hello World example
30+
31+
The following example demonstrates a minimal Go application compiled to WASI and executed using WasmEdge.
32+
33+
### hello.go
34+
35+
```go
36+
package main
37+
38+
import "fmt"
39+
40+
func main() {
41+
fmt.Println("Hello, world")
42+
}
43+
```
44+
45+
### Build
46+
47+
```bash
48+
# initialize a module (optional, but recommended)
49+
go mod init hello
50+
# build a WASI-compatible WebAssembly module
51+
GOOS=wasip1 GOARCH=wasm go build -o hello.wasm
52+
```
53+
54+
### Run with WasmEdge
55+
56+
```bash
57+
wasmedge hello.wasm
58+
```
59+
60+
Expected output:
61+
62+
```
63+
Hello, world
64+
```
65+
66+
Ensure the example prints "Hello, world" when run with WasmEdge. This section is intentionally minimal to demonstrate the native Go WASI path working end-to-end.
67+
68+
69+
## Building a WASI module with Go 1.21
70+
71+
To compile a Go application to a WASI-compatible WebAssembly module, Go 1.21 or newer is required.
72+
73+
Use the following environment variables when building your application:
74+
75+
```bash
76+
GOOS=wasip1 GOARCH=wasm go build -o app.wasm
77+
```
78+
79+
In this command:
80+
81+
GOOS=wasip1 specifies the WASI Preview 1 target
82+
83+
GOARCH=wasm selects the WebAssembly architecture
84+
85+
app.wasm is the generated WebAssembly module
86+
87+
The resulting .wasm file can be executed by WASI-compliant runtimes such as WasmEdge.
88+
89+
90+
## Running the module with WasmEdge
91+
92+
After building the WebAssembly module (for example, `app.wasm`), it can be executed using the WasmEdge CLI.
93+
94+
```bash
95+
wasmedge app.wasm
96+
```
97+
98+
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.
99+
100+
101+
## Networking and WASI limitations
102+
103+
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.
104+
105+
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.
106+
107+
108+
## Networking with stealthrocket/net
109+
110+
To work around the lack of native networking support in WASI Preview 1,
111+
alternative approaches are required. One such experimental approach is the use
112+
of the external `stealthrocket/net` project, which provides a networking
113+
implementation designed for WASI environments.
114+
115+
Rather than relying on traditional socket APIs, `stealthrocket/net` forwards
116+
network requests to host-provided functionality. This can enable limited HTTP
117+
and networking capabilities for Go applications compiled to WASI, depending on
118+
the runtime environment.
119+
120+
> **Note**
121+
> `stealthrocket/net` is **not part of WasmEdge** and is mentioned here only as an
122+
> external reference. Usage details, compatibility, and examples should be
123+
> verified directly with the project maintainers.
124+
125+
For more information, see the official repository:
126+
- https://github.com/stealthrocket/net
127+
128+
129+
130+
## Importing host functions in native Go (Go 1.21+)
131+
132+
Native Go applications compiled to WASI can call functions that are implemented
133+
by the WebAssembly runtime (such as WasmEdge) rather than inside the module
134+
itself.
135+
136+
Starting with Go 1.21, host functions can be declared using the
137+
`//go:wasmimport` directive.
138+
139+
### Example
140+
141+
```go
142+
package main
143+
144+
//go:wasmimport wasi_snapshot_preview1 proc_exit
145+
func procExit(code uint32)
146+
147+
func main() {
148+
procExit(0)
149+
}
150+
```
151+
152+
In this example, proc_exit is a WASI host function provided by the runtime.
153+
The implementation is supplied by the WASI environment rather than by Go code
154+
inside the WebAssembly module.
155+
156+
For more details, see the official Go documentation:
157+
- https://pkg.go.dev/cmd/compile#hdr-Directives

0 commit comments

Comments
 (0)