Skip to content

Commit

Permalink
docs: make the Go section of the readme clearer (#921)
Browse files Browse the repository at this point in the history
adds a command to initialize the Go directory and repalces Pirnt to HostPrint

Signed-off-by: Jiaxiao Zhou (Mossaka) <duibao55328@gmail.com>
  • Loading branch information
Mossaka authored Apr 4, 2024
1 parent b18643c commit 63bf2a9
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,13 @@ e.g. Java, Kotlin, Clojure, Scala, etc.

### Guest: TinyGo

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:
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:

`tinygo build -target=wasi main.go`

> Note: the current TinyGo bindgen only supports TinyGo version v0.27.0 or later.
> Note: the current TinyGo `bindgen` requires TinyGo version v0.27.0 or later.
To start in Go a `*.go` and `*.h` C header file are generated for your
project to use. These files are generated with the [`wit-bindgen` CLI
command][cli-install] in this repository.
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.

```sh
wit-bindgen tiny-go ./wit
Expand All @@ -338,35 +336,42 @@ wit-bindgen tiny-go ./wit
# Generating "host_component_type.o"
```

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.
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.

Some example code using this would then look like
An example of using the generated Go code would look like:

Initialize Go:
```bash
go mod init example.com
```

Create your Go main file:

```go
// my-component.go
package main

import (
gen "host/gen"
api "example.com/api"
)

func init() {
a := HostImpl{}
gen.SetHost(a)
api.SetHost(a)
}

type HostImpl struct {
}

func (e HostImpl) Run() {
gen.Print("Hello, world!")
api.HostPrint("Hello, world!")
}

//go:generate wit-bindgen tiny-go ../wit --out-dir=gen
//go:generate wit-bindgen tiny-go wit --out-dir=api
func main() {}
```

This can then be compiled with `tinygo` and assembled into a component with:
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`:

```sh
go generate # generate bindings for Go
Expand Down

0 comments on commit 63bf2a9

Please sign in to comment.