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-go
tool to generate the Go bindings from the WIT files:
$ go get -tool go.bytecodealliance.org/cmd/wit-bindgen-go
$ go tool wit-bindgen-go generate --out binding --world world-name ./wit
Compile the Go code
Using the TinyGo compiler, compile the Go code into a WebAssembly module:
$ tinygo build -target=wasip2 -wit-package ./wit -wit-world world-name -tags=purego -o go_example.wasm main.go
The resulting WASM file is a component ready to be uploaded to Golem.
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