Neon Functions
Run a Go HTTP server on Neon Functions with the functions-tinygo template.
Neon Functions is Neon’s serverless functions platform for running HTTP endpoints alongside your Neon database. The functions-tinygo template compiles your http.Handler to Wasm with TinyGo and serves it through the Neon Functions runtime — the same workers.Serve() API as on the other platforms.
Requirements
- Node.js (and npm)
- TinyGo 0.42.0 or later — TinyGo 0.41.x cannot build
net/httpfor Wasm
Create a project
npx degit github:syumai/workers-go/_templates/neon/functions-tinygo my-app
cd my-app
go mod init
go mod tidy
npm install
Start the dev server:
npm run dev
curl http://localhost:8787/hello # outputs "Hello!"
How it works
npm run build runs workers-assets-gen -runtime=neon to copy the runtime assets into ./build, then tinygo build -o ./build/app.wasm -target wasm -no-debug ./... compiles your Go code next to them. Neon Functions only loads an entry file named index.mjs or index.js (docs), so the generated worker.mjs entry is emitted as index.mjs for this runtime.
The template’s neon.ts points the worker function at ./build with bundler: "none" — the Go Wasm build output is served as-is:
import { defineConfig } from "@neon/config/v1";
export default defineConfig({
functions: {
worker: {
name: "functions-tinygo",
source: "./build",
bundler: "none",
},
},
});
On the Go side it is an ordinary http.Handler:
func main() {
http.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Hello!"))
})
workers.Serve(nil) // if nil is given, http.DefaultServeMux is used
}
Commands
| Command | Description |
|---|---|
npm run dev |
Build and run the dev server |
npm run build |
Generate runtime assets and build the Go Wasm binary |
npx neon auth |
Authenticate with Neon |
npx neon link --project-name <PROJECT_NAME> --region-id <REGION_ID> |
Create and link a Neon project |
npm run deploy |
Build and deploy the function |
To deploy, run npx neon auth and npx neon link once to connect the project to your Neon account, then npm run deploy.