Skip to content

Commit

Permalink
Merge pull request #175 from Mossaka/mossaka/fix-go-docs
Browse files Browse the repository at this point in the history
component-model/src/language-support/go: fix go docs
  • Loading branch information
kate-goldenring authored Nov 15, 2024
2 parents 683f208 + 32efb42 commit 088fe2a
Showing 1 changed file with 11 additions and 49 deletions.
60 changes: 11 additions & 49 deletions component-model/src/language-support/go.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ Optional: Install the `wkg` CLI tool to resolve the imports in the WIT file. The

## 2. Determine which World the Component will Implement

The `wasip2` target of TinyGo assumes that the component is targeting `wasi:cli/[email protected]` world so it requires the imports of `wasi:cli/[email protected]`. We need to include them in the `add.wit`. Tools like `wkg` can be handy to build a complete WIT package by resolving the imports.
The `wasip2` target of TinyGo assumes that the component is targeting `wasi:cli/[email protected]` world so it requires the imports of `wasi:cli/[email protected]`. We need to include them in the `add.wit`.

Tools like `wkg` can be convenient to build a complete WIT package by resolving the imports.

```wit
# wit/add.wit
Expand All @@ -34,55 +36,15 @@ world adder {
Running the `wkg wit build` command will resolve the imports and generate the complete WIT file encoded as a Wasm component.

```console
wkg wit build
$ wkg wit build
WIT package written to docs:[email protected]
```

Or you can manually include the required imports in the `add.wit` file. Below is the minimal `add.wit` file that includes the required imports:

```wit
package docs:[email protected];
world adder {
import wasi:io/[email protected];
import wasi:io/[email protected];
import wasi:cli/[email protected];
import wasi:random/[email protected];
export add: func(x: s32, y: s32) -> s32;
}
package wasi:[email protected] {
interface error {
resource error;
}
interface streams {
use error.{error};
resource output-stream {
blocking-write-and-flush: func(contents: list<u8>) -> result<_, stream-error>;
}
variant stream-error {
last-operation-failed(error),
closed,
}
}
}
package wasi:[email protected] {
interface stdout {
use wasi:io/[email protected].{output-stream};
The `docs:[email protected]` file is a Wasm encoding of the WIT package. Next, we can generate the bindings for it:

get-stdout: func() -> output-stream;
}
}
package wasi:[email protected] {
interface random {
get-random-u64: func() -> u64;
}
}
```console
$ go get go.bytecodealliance.org/cmd/wit-bindgen-go
$ go run go.bytecodealliance.org/cmd/wit-bindgen-go generate -o internal/ ./docs:[email protected]
```

Now, create your Go project:
Expand All @@ -96,10 +58,10 @@ Next, we can generate the bindings for the `add.wit` file:

```console
$ go get go.bytecodealliance.org/cmd/wit-bindgen-go
$ go run go.bytecodealliance.org/cmd/wit-bindgen-go generate -o internal/ ./add.wit
$ go run go.bytecodealliance.org/cmd/wit-bindgen-go generate -o internal/ ./docs:[email protected]
```

The `internal` directory will contain the generated Go code for the `add.wit` file.
The `internal` directory will contain the generated Go code that WIT package.

```console
$ tree internal
Expand Down

0 comments on commit 088fe2a

Please sign in to comment.