Go Tooling
This guide walks through building a Go component that implements
the adder world defined in the adder/world.wit package.
The component will implement the adder world,
which contains an add interface with an add function.
Keep in mind that this is a basic intro. For more examples, please see the componentize-go repository examples.
If you still have questions, feel free to reach out to to the componentize-go maintainers on Zulip or open an issue on the repository.
1. Install the tools
- Go 1.25.5+
- componentize-go Latest version
2. Create your Go project
With the component-docs repository cloned locally, run the following:
mkdir go-adder && cd go-adder
2. Determine which world the component will implement
Since we will be implementing the adder world, we can copy the WIT to our project.
Create a subdirectory called wit and paste the following code
into a file called wit/adder.wit:
package docs:adder@0.1.0;
interface add {
add: func(x: u32, y: u32) -> u32;
}
world adder {
export add;
}
3. Generate bindings for the Wasm component
Run the following commands to generate bindings for the component:
componentize-go --world adder bindings
go mod tidy
The project directory will look like this:
$ tree
.
├── go.mod
├── go.sum
├── wit
│ └── adder.wit
└── wit_exports.go
Here’s a breakdown of what each of these files do:
go.modcontains a required library of shared WIT types (see go.bytecodealliance.org/pkg)wit_exports.godefines the//go:wasmexportmethods for the Go WebAssembly compiler.
If you try to compile this, you’ll get an error from the wit_exports.go file:
could not import wit_component/export_docs_adder_add (no required module provides package "wit_component/export_docs_adder_add")
We’ll create this module in the next section.
4. Implement the add Function
In your add directory, create a file called export_docs_adder_add/exports.go
and paste the following code into it:
package export_docs_adder_add
func Add(x uint32, y uint32) uint32 {
return x + y
}
5. Build the Component
To compile your Go application to WebAssembly, run the following command:
componentize-go build
This will output a main.wasm file that will be run in the next section.
5. Testing the add Component
To verify that our component works, let’s run it from a Rust application that knows how to run a
component targeting the adder world. Be sure to have the Rust toolchain
installed.
The application uses wasmtime to generate Rust “host”/“embedder” bindings,
bring in WASI worlds, and execute the component.
From within the go-adder directory, run the following:
$ cd ../component-model/examples/example-host
$ cargo run --release -- 1 2 ../../../go-adder/main.wasm
1 + 2 = 3
With this, we have successfully built and run a basic WebAssembly component with Go 🎉