Building Golem Components in Go
Building Golem components having an application manifest is straightforward, just use the golem
command line interface:
golem app build
If the project was created using golem app new
as recommended, the golem app build
command will always work as expected.
The result of the golem app build
command is a WebAssembly component file ready to be uploaded to Golem. It does not have to be specified explicitly, as the golem
tool will automatically find the correct file when doing for example:
golem component add
Under the hood
Building Golem components written in Go requires a series of steps.
In details, building the component requires the following steps:
Generate the Go bindings
Use the wit-bindgen
tool to generate the Go bindings from the WIT files:
$ wit-bindgen tiny-go --out-dir go_example ./wit
Compile the Go code
Using the TinyGo compiler, compile the Go code into a WebAssembly module:
$ tinygo build -target=wasi -tags=purego -o go_example.module.wasm main.go
Embed the WIT files in the WebAssembly module
The following command embeds the WIT files into the WebAssembly module:
$ wasm-tools component embed ./wit go_example.module.wasm --output go_example.embed.wasm
Package the Go code into a WASM component
Finally use wasm-tools
to create the WebAssembly component:
$ wasm-tools component new go_example.embed.wasm -o go_example.wasm --adapt adapters/tier1/wasi_snapshot_preview1.wasm
The adapter WASM can be downloaded from the golem-wit repository (opens in a new tab).
Reducing the component's size
The generated component's size can be reduced by stripping some parts of it using the wasm-tools strip
command:
$ wasm-tools strip go_example.wasm -o go_example_stripped.wasm