Welcome to the new Golem Cloud Docs! 👋
Documentation
Go Language Guide
Building Components

Building Golem Components in Go

Building Golem components written in Rust as it is described on the Building Components page requires a series of steps.

If the project was created with golem-cli new, it already has a Makefile that incorporates all the necessary steps to build the component, so it is enough to run:

$ make

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 --world go-example --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 --world go-example-imports --output go_example.embed.wasm
⚠️

Note that when using the Golem Go SDK, a different world must be used for wit-bindgen and wasm-tools component embed commands, as explained on the Golem Go SDK page.

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