Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Go example #195

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions component-model/src/language-support/go.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,14 @@ $ wkg wit build
WIT package written to docs:[email protected]
```

The `docs:[email protected]` file is a Wasm encoding of the WIT package. Next, we can generate the bindings for it:

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

```console
$ mkdir add && cd add
$ go mod init example.com
```

Next, we can generate the bindings for the `add.wit` file:
Next, we can generate the bindings for the Wasm component:

```console
$ go get go.bytecodealliance.org/cmd/wit-bindgen-go
Expand Down Expand Up @@ -104,11 +97,11 @@ The `adder.exports.go` file contains the exported functions that need to be impl
package main

import (
"example.com/internal/example/component/example"
"example.com/internal/docs/adder/adder"
)

func init() {
example.Exports.Add = func(x int32, y int32) int32 {
adder.Exports.Add = func(x int32, y int32) int32 {
return x + y
}
}
Expand All @@ -127,7 +120,7 @@ We can build our component using TinyGo by specifying the wit-package to be `add
Under the hood, TinyGo invokes `wasm-tools` to embed the WIT file to the module and componentize it.

```console
$ tinygo build -target=wasip2 -o add.wasm --wit-package add.wit --wit-world adder main.go
$ tinygo build -target=wasip2 -o add.wasm --wit-package docs:[email protected] --wit-world adder main.go
```

We now have an add component that satisfies our `adder` world, exporting the `add` function, which
Expand Down