scaffold-go-microservice
Skill1 starsChanged 7 months agoMITView on GitHub
Scaffold a production-ready Go HTTP microservice — chi router + sqlc + golang-migrate + structured logging + OpenTelemetry + healthchecks + Dockerfile + GitHub Actions. Use when starting a new Go service, not when adding structure to an existing one.
In this repository50 other files it gives its agents.
- algolia-searchSkill
- audit-tautological-testsSkill
- blame-archaeologySkill
- brevo-emailSkill
- bun-runtimeSkill
- clerk-authSkill
- cloudflare-workersSkill
- dbt-data-modelingSkill
- explain-this-diffSkill
- extract-skill-from-sessionSkill
- fastapi-productionSkill
- find-dead-codeSkill
- find-real-bugSkill
- gdpr-dpiaSkill
- github-actions-ciSkill
- graphql-relaySkill
- grpc-servicesSkill
- hipaa-auditSkill
- india-dpdp-actSkill
- java-8-to-21Skill
- jest-to-vitestSkill
- kubernetes-helmSkill
- mongo-to-postgresSkill
- nextjs-pages-to-appSkill
- node-version-upgradeSkill
- opentelemetry-instrumentSkill
- pci-dss-checklistSkill
- plan-the-rollbackSkill
- python-2-to-3Skill
- razorpay-integrationSkill
- react-native-expoSkill
- scaffold-cli-toolSkill
- scaffold-fullstack-appSkill
- scaffold-new-appSkill
- scaffold-saas-starterSkill
- sentry-monitoringSkill
- shrink-this-prSkill
- soc2-evidenceSkill
- spec-from-conversationSkill
- stripe-integrationSkill
- supabase-backendSkill
- terraform-patternsSkill
- threat-modeling-strideSkill
- triage-stack-traceSkill
- wcag-accessibility-auditSkill
- webpack-to-viteSkill
- write-adrSkill
- write-commit-messageSkill
- write-pr-descriptionSkill
- write-runbookSkill
The file
We split this skill into the rules it gives an agent
1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 191
If your SQL has a typo, make sqlc fails at *codegen time*, not runtime.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Scaffold a Go Microservice
The opinionated "this is the layout you'd land on after building 3 Go services" scaffold. No frameworks beyond chi. No code generators that create magic. Idiomatic Go that an experienced reader recognizes immediately.
When to use
- New Go service, day 1.
- Internal service or public API; HTTP/JSON.
- You want sqlc (typed SQL) over GORM (ORM with reflection cost).
- You'll deploy to Kubernetes or Cloud Run / Fly / Railway.
When NOT to use
- gRPC-first service → use
grpc-servicesskill (different scaffold). - Tiny CLI tool → use
scaffold-cli-tool. - Adding structure to an existing service — different problem.
- You want a heavy framework (Gin, Fiber, Echo) — this scaffold is
net/http+ chi.
Decisions made for you
| Decision | Choice | Why |
| Router | go-chi/chi | Stdlib-shaped middleware, fastest mental model |
| DB access | sqlc (codegen from .sql) | Compile-time SQL safety, no ORM magic |
| Migrations | golang-migrate | Dialect-agnostic, plays well with CI |
| Config | kelseyhightower/envconfig | Struct + tags > viper for service-shaped configs |
| Logging | slog (stdlib, Go 1.21+) | Structured, levelled, zero deps |
| Tracing/Metrics | OpenTelemetry SDK + OTLP exporter | One vendor, all signals |
| Errors | errors.Is/As + sentinel errors | No pkg/errors, no custom error stacks |
| Tests | stdlib testing + testify/require | Familiar, minimal |
| Docker | distroless base, multi-stage | 20MB images, no shell |
| CI | GitHub Actions | Most teams have it |
File structure
my-service/
cmd/
server/main.go # entrypoint, wires deps
internal/
api/ # HTTP handlers (one file per resource)
health.go
user.go
middleware.go
domain/ # business logic, no HTTP/DB types
user.go
db/
queries/ # *.sql files for sqlc
migrations/ # *.up.sql / *.down.sql
sqlc.yaml
generated/ # sqlc output (committed)
config/
config.go # envconfig struct
obs/
logger.go
tracing.go
Dockerfile
docker-compose.yml # local postgres + otel collector
Makefile # run, test, lint, sqlc, migrate
.golangci.yml
go.mod
README.md
.github/workflows/ci.ymlinternal/ over pkg/ — these are private to the service. pkg/ invites accidental public APIs.
main.go pattern (the whole service in one readable file)
// cmd/server/main.go
package main
import (
"context"
"errors"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/kelseyhightower/envconfig"
"myservice/internal/api"
"myservice/internal/config"
"myservice/internal/db/generated"
"myservice/internal/obs"
)
func main() {
if err := run(); err != nil {
slog.Error("fatal", "err", err)
os.Exit(1)
}
}
func run() error {
var cfg config.Config
if err := envconfig.Process("", &cfg); err != nil {
return err
}
logger := obs.NewLogger(cfg.Env)
slog.SetDefault(logger)
shutdownTracing, err := obs.SetupTracing(context.Background(), cfg)
if err != nil {
return err
}
defer shutdownTracing()
pool, err := pgxpool.New(context.Background(), cfg.DatabaseURL)
if err != nil {
return err
}
defer pool.Close()
queries := generated.New(pool)
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(api.RequestLogging(logger))
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(30 * time.Second))
api.MountHealth(r, pool)
api.MountUsers(r, queries)
srv := &http.Server{
Addr: cfg.Addr,
Handler: r,
ReadHeaderTimeout: 5 * time.Second,
}
go func() {
slog.Info("listening", "addr", cfg.Addr)
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("listen failed", "err", err)
os.Exit(1)
}
}()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
<-stop
slog.Info("shutting down")
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
return srv.Shutdown(ctx)
}A new dev should read this and immediately know: where config comes from, where the DB connects, where routes mount, how shutdown works. No framework magic.
sqlc — the killer feature
internal/db/queries/users.sql:
-- name: GetUser :one
SELECT * FROM users WHERE id = $1;
-- name: CreateUser :one
INSERT INTO users (email, name) VALUES ($1, $2) RETURNING *;
-- name: ListActiveUsers :many
SELECT * FROM users WHERE deleted_at IS NULL ORDER BY created_at DESC LIMIT $1 OFFSET $2;make sqlc generates internal/db/generated/users.sql.go with typed Go functions:
user, err := queries.GetUser(ctx, userID) // user is users.User struct
created, err := queries.CreateUser(ctx, generated.CreateUserParams{
Email: "x@y.com",
Name: pgtype.Text{String: "Mahesh", Valid: true},
})If your SQL has a typo, make sqlc fails at codegen time, not runtime. ORMs ship the typo.
Healthchecks (the version K8s wants)
// internal/api/health.go
func MountHealth(r chi.Router, pool *pgxpool.Pool) {
// Liveness: are we running? Cheap.
r.Get("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
// Readiness: can we serve traffic? Checks deps.
r.Get("/readyz", func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
defer cancel()
if err := pool.Ping(ctx); err != nil {
http.Error(w, "db down", http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK)
})
}K8s docs and most blog posts conflate these. They are different signals: liveness restarts the pod; readiness yanks it from the load balancer. Get them right or you'll have a service that never gets traffic, or a service that thrashes-restarts forever.
Dockerfile (distroless, multi-stage)
FROM golang:1.23 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /out/server ./cmd/server
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/server /server
USER nonroot:nonroot
EXPOSE 8080
ENTRYPOINT ["/server"]20MB image, no shell, no package manager, runs as nonroot. Audit findings shrink dramatically.
What the scaffold emits
- The directory tree above.
cmd/server/main.goready to run with exampleusersresource.- sqlc.yaml + sample query + matching migration.
- Dockerfile + docker-compose with Postgres + OTel collector.
- Makefile with
run,test,lint,sqlc,migrate-up,migrate-down,migrate-create. .golangci.ymlwith sane defaults (errcheck, govet, gosimple, ineffassign, staticcheck, gosec).- GitHub Actions: lint, test (with race detector), sqlc verify, migration verify, docker build.
- README with the "first 5 minutes" runbook.
Anti-patterns
- GORM/Ent for new services — reflection cost, query opacity, and you'll fight it within 6 months.
sqlcinstead. pkg/instead ofinternal/— accidentally publishes types. Useinternal/.- Heavy frameworks (Gin, Fiber, Echo) — they wrap
net/httpfor ergonomics you don't need. Stdlib + chi is enough. - Initializing OTel without graceful shutdown — drops in-flight spans. The defer matters.
- One
/healthfor both liveness and readiness — guarantees a bad K8s deployment day. Split them. - Logging
fmt.Println/log.Printf— unstructured logs are unsearchable in production.slogeverywhere. time.Now()in domain code — untestable. Inject aClockinterface, default to real time.- Goroutines without context cancellation — leaks on shutdown. Every
go func()accepts actx. - DB queries without
ctx— request timeout doesn't propagate; pool exhaustion under load.
Verify it worked
make runstarts the service against the docker-compose Postgres;curl /healthzreturns 200.- Killing the service with
Ctrl+Cshuts down cleanly within 15s (no panics, no leaked goroutines). make sqlcregenerates Go from SQL with no diff (or expected diff if SQL changed).make testpasses with-raceenabled.make lintruns golangci-lint with zero issues on the scaffold.- Docker image builds < 30s, final size < 30MB.
/readyzreturns 503 when Postgres is killed;/healthzkeeps returning 200.- An OTel trace shows the full request path: HTTP → DB query, with parent-child relationship intact.
- CI green on first push.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

