Community Perk$300 in Akamai Cloud credits for buildersClaim your credits

Go on Spin 4: Build Wasm Components with componentize-go

For years, running Go on Spin meant TinyGo. That is no longer the case.

The Spin 4.x language guide for Go states that TinyGo is “no longer needed or recommended for new projects”, because standard Go can now target WASI. 🎉

The missing piece is componentize-go, a project from the Bytecode Alliance that turns a Go module into a Wasm component. In this article, I’ll build a Spin 4 HTTP app with it, call an external API, and show what to change in an existing TinyGo-based app.

Why TinyGo Is No Longer The Default

TinyGo compiled a subset of Go with its own compiler and runtime. It produced small binaries, but not every standard library package or language feature worked. componentize-go takes the opposite approach: you write regular Go, and the tool wraps the compiled output into a Wasm component for a given WIT world. For Spin, the templates take care of that world, so you rarely have to think about it.

Prerequisites And Project Setup

You need the Spin CLI (version 4.3 or newer, which ships the updated templates) and a recent Go toolchain. The Spin templates currently pin go 1.27.1 in go.mod, and the componentize-go README asks for Go 1.27.1 or newer. The Spin docs page still mentions 1.25.5, so go with the newer.

If you haven’t installed the Go templates yet:

spin templates install --git https://github.com/spinframework/spin --update

Then scaffold the app from the http-go template:

# Create a new Spin app using the http-go template
spin new -t http-go hello-go -a
cd hello-go

The template wires componentize-go into go.mod as a Go tool:

tool github.com/bytecodealliance/componentize-go

That means there is nothing to install globally. go tool componentize-go always runs the version pinned by your module.

Writing The HTTP Handler

The handler is registered in init. It uses the standard http.ResponseWriter and *http.Request types, and main functions remains empty:

package main

import (
    "fmt"
    "net/http"

    spinhttp "github.com/spinframework/spin-go-sdk/v4/http"
)

func init() {
    spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "text/plain")
        fmt.Fprintln(w, "Hello from Go on Spin 4")
    })
}

func main() {}

Note the /v4 in the import path. The docs page still shows /v3, but Spin 4.3 moved its Go templates to spin-go-sdk v4, a major bump caused by changes to the generated bindings. According to the PR, the high-level API is stable from v3, so your handler code stays the same.

Build And Run

The manifest generated by the template points spin build at componentize-go:

[component.hello-go]
source = "main.wasm"
allowed_outbound_hosts = []

[component.hello-go.build]
command = "go tool componentize-go build"

Build and run the app:

# Compile & Run The App
spin up --build

Spin listens on localhost:3000 by default, so curl localhost:3000 reaches the handler.

Outbound HTTP And allowed_outbound_hosts

The SDK ships helpers for outgoing requests: Get, Post, Send, and NewClient, which returns a regular *http.Client. Here is a handler that proxies a public API:

func init() {
    spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {
        resp, err := spinhttp.Get("https://random-data-api.fermyon.app/animals/json")
        if err != nil {
            http.Error(w, err.Error(), http.StatusBadGateway)
            return
        }
        defer resp.Body.Close()

        w.Header().Set("Content-Type", "application/json")
        io.Copy(w, resp.Body)
    })
}

Add "io" to the imports. Run it as is and the call fails with a “Destination not allowed” error: Spin denies all outbound traffic that the manifest doesn’t list. Allow the host in spin.toml:

[component.hello-go]
source = "main.wasm"
allowed_outbound_hosts = ["https://random-data-api.fermyon.app"]

Migrating A TinyGo Spin App

For an existing app, four things change:

  1. Replace the tinygo build command in [component.<name>.build] with go tool componentize-go build.
  2. Add tool github.com/bytecodealliance/componentize-go to go.mod and run go mod tidy.
  3. Move to spin-go-sdk v4 and update the import paths:
-spinhttp "github.com/spinframework/spin-go-sdk/v3/http"
+spinhttp "github.com/spinframework/spin-go-sdk/v4/http"
  1. Bump the go directive to a toolchain version that satisfies componentize-go.

The handler registration with spinhttp.Handle doesn’t change, and neither does the manifest apart from the build command.

Limits To Know

componentize-go is a young tool with 0.x versions, so expect rough edges and pin its version through go.mod. HTTP is the default case. For any other trigger, the build must name the WIT world explicitly.

The Redis template, for example, builds with go tool componentize-go --world spin:up/redis-trigger@4.0.0 build. The templates set this up for you, but it’s good to know where it lives when you hand-write a manifest.

Recap

Although TinyGo served use pretty well, I’m super happy that we can finally write Spin applications with standard (or big) Go. A tool directive in go.mod, go tool componentize-go build as the build command, and the recently released spin-go-sdk/v4 make it possible.

Thorsten Hans
Thorsten Hans
Sr. Developer Advocate

Thorsten Hans is a Senior Developer Advocate at Akamai, Docker Captain, and Wasm enthusiast. He thrives on pushing the limits of WebAssembly and Edge Computing to help developers build high-performance distributed systems. Thorsten shares his technical deep-dives via his blog and on global stages to shape the cloud's future.