-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #175 from Mossaka/mossaka/fix-go-docs
component-model/src/language-support/go: fix go docs
- Loading branch information
Showing
1 changed file
with
11 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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: | ||
|
@@ -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 | ||
|