Go Component SDK with Custom WIT
This example is a WebAssembly component that demonstrates how to use the wasmCloud Go Component SDK in conjunction with your own custom WIT interfaces.
You can find this application's custom interface in wit/world.wit
:
interface invoker {
call: func() -> string;
}
📦 Dependencies
Before starting, ensure that you have the following installed in addition to the Go (1.23+) toolchain:
tinygo
for compiling Go (always use the latest version)wasm-tools
for Go bindings- wasmCloud Shell (
wash
) for building and running the components and wasmCloud environment
👟 Run the example
Clone the wasmCloud/go repository:
git clone https://github.com/wasmCloud/go.git
Change directory to examples/component/invoke
:
cd examples/component/invoke
In addition to the standard elements of a Go project, the example directory includes the following files and directories:
build/
: Target directory for compiled.wasm
binariesgen/
: Target directory for Go bindings of interfaceswit/
: Directory for WebAssembly Interface Type (WIT) packages that define interfacesbindings.wadge_test.go
: Automatically generated test bindingswadm.yaml
: Declarative application manifestwasmcloud.lock
: Automatically generated lockfile for WIT packageswasmcloud.toml
: Configuration file for a wasmCloud application
Build the component
We will build and deploy this example manually, since we will be using the wash call
subcommand to interact with the application, requiring a stable identity to call. (As you gain experience with wasmCloud, you will likely want to use the wash dev
subcommand to automate your development process.)
Build the component:
wash build
Start a wasmCloud environment
Start a local wasmCloud environment (using the -d
/--detached
flag to run in the background):
wash up -d
Deploy the application
Deploy the component using the application manifest (wadm.yaml
):
wash app deploy wadm.yaml
To ensure that the application has reached Deployed
status, you can use wash app list
:
wash app list
Invoke the component
Once the application is deployed, you can call the component using the wash call
subcommand, which invokes a function on a component:
wash call invoke_example-invoker example:invoker/invoker.call
Hello from the invoker!
Clean up
You can delete an application from your wasmCloud environment by referring either to its application name (invoke-example
) or the original application manifest:
wash app delete wadm.yaml
Stop your local wasmCloud environment:
wash down
Bonus: Calling when running with wash dev
When running with wash dev
, wasmCloud uses a generated ID for the component. If you have jq
installed,
you can run the following command to call the component:
wash call "$(wash get inventory -o json | jq -r '.inventories[0].components[0].id')" example:invoker/invoker.call
Hello from the invoker!
📖 Further reading
For more on custom interfaces, see the Interface Developer Guide in the wasmCloud documentation.