Add a Go Module Dependency
Important constraints
- The component compiles to a WebAssembly component (WASI) via
componentize-go— only code that works on that target will build. - Modules that use OS threads, raw sockets (
net.Dial), most database drivers,cgo, or platform-specific syscalls will not work. - Pure-Go modules, and modules that do I/O through the standard library the SDK adapts (e.g. HTTP via
net/http), generally work. - If unsure whether a module builds for WASM, add it and run
golem buildto find out.
Steps
-
Add the dependency in the component’s module directory (where its
go.modlives):go get github.com/example/mod@latestThen import and use it in your code.
go getrecords it ingo.mod. -
Build to verify
golem build --yesgolem buildrunsgo mod tidyfor you before compiling. Do NOT rely on a barego buildfor the component — it can’t link the WASI component; always usegolem build. -
If the build fails
- Look for
wasip1/wasm-unsupported errors, references tonet,syscall, or C dependencies — those modules are incompatible with the WASM target. - Check whether the module offers a pure-Go or
wasmbuild mode/build tag. - Look for an alternative module that supports WASM.
- Look for
Already available (do NOT re-add)
github.com/golemcloud/golem/sdks/go/golem— the Golem Go SDK (agents, durability, RPC, retry, config/secrets, keyvalue/blobstore/rdbms/websocket, logging). Its subpackages (.../golem/retry,.../golem/keyvalue, etc.) are part of the same module.github.com/bytecodealliance/componentize-go— pinned as atooldependency ingo.modsogo tool componentize-goruns the pinned version. Leave thetooldirective in place; don’t remove it.
HTTP and networking
Use the standard net/http package — the SDK routes it through the durable WASI HTTP transport (see golem-make-http-request-go). Raw sockets (net.Dial, crypto/tls over a custom conn) are not available on the WASM target.
Related Guides
| Guide | Description |
|---|---|
golem-make-http-request-go | Call an external service over HTTP |
golem-add-agent-go | Use a new dependency inside an agent |