Use Case: Go + WebAssembly on Cloudflare
Go can compile to WebAssembly, which opens the door to running Go code in the browser — but it’s worth understanding the trade-offs before reaching for it, especially on Cloudflare’s platform.
Compiling Go to Wasm
Section titled “Compiling Go to Wasm”GOOS=js GOARCH=wasm go build -o main.wasm main.goYou’ll also need the small JS support glue that ships with the Go toolchain:
cp "$(go env GOROOT)/lib/wasm/wasm_exec.js" .A minimal main.go that exposes a function to JavaScript:
package main
import "syscall/js"
func add(this js.Value, args []js.Value) any { a := args[0].Int() b := args[1].Int() return a + b}
func main() { js.Global().Set("goAdd", js.FuncOf(add)) <-make(chan struct{}) // keep the program alive}Load it from HTML:
<script src="wasm_exec.js"></script><script> const go = new Go(); WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject) .then((result) => go.run(result.instance));</script>The catch: binary size and cold start
Section titled “The catch: binary size and cold start”The standard Go-to-Wasm toolchain produces binaries in the multiple-megabytes range, even for tiny programs, because it bundles the Go runtime and garbage collector. That’s fine for a browser tab loading once, but it’s a poor fit for Cloudflare Workers, where:
- Workers run on the isolate model (V8), not a general Wasm sandbox with a POSIX-like host —
syscall/jsdoesn’t apply the same way it does in a browser. - Cold-start latency and bundle size limits make multi-MB Wasm binaries expensive compared to native JS/TS or the
workers-rs(Rust) approach, which produces much smaller Wasm output. - TinyGo, a separate Go compiler focused on embedded/Wasm targets, produces dramatically smaller binaries and is the more realistic option if you want Go logic running inside a Worker.
A more realistic split
Section titled “A more realistic split”For most projects, the practical architecture is:
- Cloudflare Pages/Workers (JS/TS, or TinyGo-compiled Wasm for CPU-bound logic) for the edge-facing frontend and lightweight request handling.
- A Go backend service (this whole site’s basics/intermediate/advanced chapters) running on a normal server or container platform for anything that needs the full standard library, database drivers, or heavier compute.