Skip to content

Commit 63bf2a9

Browse files
authored
docs: make the Go section of the readme clearer (#921)
adds a command to initialize the Go directory and repalces Pirnt to HostPrint Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]>
1 parent b18643c commit 63bf2a9

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

README.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -320,15 +320,13 @@ e.g. Java, Kotlin, Clojure, Scala, etc.
320320

321321
### Guest: TinyGo
322322

323-
Go code can be compiled for the `wasm32-wasi` target using the [TinyGo](https://tinygo.org/) compiler. For example, the following command compiles `main.go` to a wasm modules with WASI support:
323+
You can compile Go code into a Wasm module using the [TinyGo](https://tinygo.org/) compiler. For example, the following command compiles `main.go` to a WASI module:
324324

325325
`tinygo build -target=wasi main.go`
326326

327-
> Note: the current TinyGo bindgen only supports TinyGo version v0.27.0 or later.
327+
> Note: the current TinyGo `bindgen` requires TinyGo version v0.27.0 or later.
328328
329-
To start in Go a `*.go` and `*.h` C header file are generated for your
330-
project to use. These files are generated with the [`wit-bindgen` CLI
331-
command][cli-install] in this repository.
329+
When using `wit-bindgen tiny-go` bindgen, `*.go` and `*.h` C header file are generated for your project. These files are generated with the [`wit-bindgen` CLI command][cli-install] in this repository.
332330

333331
```sh
334332
wit-bindgen tiny-go ./wit
@@ -338,35 +336,42 @@ wit-bindgen tiny-go ./wit
338336
# Generating "host_component_type.o"
339337
```
340338

341-
If your Go code uses `result` or `option` type, a second Go file `host_types.go` will be generated. This file contains the Go types that correspond to the `result` and `option` types in the WIT file.
339+
If your Go code uses `result` or `option` type, an additional Go file `host_types.go` will be generated. This file contains the Go types that correspond to the `result` and `option` types in the WIT file.
342340

343-
Some example code using this would then look like
341+
An example of using the generated Go code would look like:
342+
343+
Initialize Go:
344+
```bash
345+
go mod init example.com
346+
```
347+
348+
Create your Go main file:
344349

345350
```go
346351
// my-component.go
347352
package main
348353

349354
import (
350-
gen "host/gen"
355+
api "example.com/api"
351356
)
352357

353358
func init() {
354359
a := HostImpl{}
355-
gen.SetHost(a)
360+
api.SetHost(a)
356361
}
357362

358363
type HostImpl struct {
359364
}
360365

361366
func (e HostImpl) Run() {
362-
gen.Print("Hello, world!")
367+
api.HostPrint("Hello, world!")
363368
}
364369

365-
//go:generate wit-bindgen tiny-go ../wit --out-dir=gen
370+
//go:generate wit-bindgen tiny-go wit --out-dir=api
366371
func main() {}
367372
```
368373

369-
This can then be compiled with `tinygo` and assembled into a component with:
374+
This setup allows you to invoke `go generate`, which generates the bindings for the Go code into an `api` directory. Afterward, you can compile your Go code into a WASI module using the TinyGo compiler. Lastly you can componentize the module using `wasm-tools`:
370375

371376
```sh
372377
go generate # generate bindings for Go

0 commit comments

Comments
 (0)