Getting Started
Prerequisites
Section titled “Prerequisites”- Go 1.22+ —
go version - TinyGo —
brew install tinygo - Zig 0.15+ — ziglang.org/download
- wasm-ld — comes with LLVM/Homebrew LLD
- Node.js 18+ — for wrangler
Install
Section titled “Install”git clone https://github.com/user/gomode.gitcd gomodenpm install# Build Zig .o + TinyGo into single WASM binarynpm run build
# Or separately:npm run build:zig # Zig → zig-abi.onpm run build:go # TinyGo + zig-abi.o → go.wasmnpm run dev# → http://localhost:8787Routes:
http://localhost:8787/— Hello world (Worker mode)http://localhost:8787/simd— SIMD demo (Go calling Zig)http://localhost:8787/json— JSON responsehttp://localhost:8787/do/— Durable Object mode
Project structure
Section titled “Project structure”worker/src/ worker.ts — Entry point, routes to Worker or DO go-do.ts — Durable Object runtime
zig-abi/src/ main.zig — Memory mgmt + SIMD exports simd.zig — WASM SIMD v128 batch operations
go-sdk/ gomode.go — CGo wrappers for Zig functions zig_abi.h — C header for Zig exports
examples/ hello-worker/ — Example Go handler with SIMDYour first handler
Section titled “Your first handler”Create a new directory:
mkdir my-handler && cd my-handlergo mod init my-handlerAdd go-sdk dependency:
require gomode v0.0.0replace gomode => ../go-sdkWrite your handler:
package main
import ( "gomode" "unsafe")
const ( tagI32 = 2 tagString = 4 valueSlot = 16 stringHeader = 4 respSize = 3 * valueSlot)
var respBase uintptrvar respStrings [][]byte
// zerobuf read/write helpers (see examples/hello-worker/main.go)
//export handle_zerobuffunc handleZerobuf(reqBase uint32) uint32 { path := readZBString(uintptr(reqBase) + 1*valueSlot)
switch path { case "/": return writeResponse(200, "text/plain", "Hello!") case "/sum": data := []float64{1, 2, 3, 4} sum := gomode.ZigSimdSumF64( uint32(uintptr(unsafe.Pointer(&data[0]))), uint32(len(data)), ) return writeResponse(200, "text/plain", formatFloat(sum)) default: return writeResponse(404, "text/plain", "not found") }}
func main() {}Build and run:
cd /path/to/gomodenpm run build:zigtinygo build -target wasip1 -scheduler=none -gc=leaking \ -ldflags="-extldflags='build/zig-abi.o'" \ -o worker/src/go.wasm /path/to/my-handlernpm run dev