Go API Service
Scaffold a production-ready Go API service: the handlers → service → repository layout, config, structured logging, middleware, metrics, and tracing, plus a Makefile that runs it locally with MySQL, Redis, and a Grafana observability stack. All generated Go follows conventions-go and its service architecture; the templates here are the files to generate, not a second set of rules.
- Ask for the service name and module path: a kebab-case name such as
user-service, and a module path such asgithub.com/myorg/myservice. Derive{SERVICE_NAME}(kebab-case),{SERVICE_NAME_SNAKE},{SERVICE_NAME_PASCAL}, and{MODULE_PATH}from them. Done when all four placeholders have values. - Generate the project in the layout below, resolving every placeholder.
Use the templates where one exists; write the other packages to the
contracts below, so they fit the templates that call them. Give every
package a
doc.goand a shortREADME.md, as conventions-go requires. Done when every file in the layout exists and no{…}placeholder is left. - Initialise and check it. Create
go.modas go-mod.md describes, then runmake fmt,make lint-all, andmake test. Done when all three pass.
Parallel scaffold. The placeholder map is fixed, so instantiating the
templates is a
verified-pattern fan-out: generate one
representative package (internal/api) first and confirm the substitutions
and layout with the user, then have subagents instantiate the rest in
parallel, following the shared completion and retry protocol.
Layout
txt{SERVICE_NAME}/ ├── cmd/api/main.go ├── internal/ │ ├── api/ config/ contextx/ deps/ env/ errorsx/ httpx/ │ └── logx/ metrics/ middleware/ mysql/ redis/ traces/ ├── docs/ │ ├── projects/completed/ │ ├── projects/README.md │ └── architecture.md ├── e2e/ ├── local/ │ ├── config/ mysql/ observability/ ├── scripts/ ├── go.mod go.sum Makefile AGENTS.md README.md
Packages that shadow a standard-library name take an x suffix: logx,
httpx, errorsx, contextx, timex.
Templates
cmd/apiandinternal/api: internal-api.mdinternal/config: internal-config.mdinternal/logx: internal-logx.mdinternal/middleware: internal-middleware.mdgo.mod: go-mod.mdMakefile: makefile.mdlocal/config: config-local.mdlocal/mysql: docker-compose-mysql.mdlocal/observability: docker-compose-obs.md
Contracts for packages without a template
The templates call these, so write each package to match:
contextx:WithComponent(ctx, name) context.Context,RoutePatternContextKey,RoutePatternFromContext(ctx) (string, bool).deps:Dependencies{MySQLFailed, RedisFailed bool}.env:type Environment string, andAll []Environmentholdinglocal,dev,stg, andprd.errorsx: domain sentinels such asErrNotFound, andNewProblem(title, detail)for RFC 7807 responses.httpx:WriteJSON(ctx, logger, w, status, v).metrics:New(registry, component, logger) *Metrics,StoreCreate(*Store),StartServer(ctx, logger, host, port) *http.Server,RegisterGauge,Count,RequestsTotal,RequestDuration,RequestSize,ResponseSize, andWithHealthCheckMetrics.mysql:New(ctx, config.MySQL, logger) (*sql.DB, error), reading the password fromMYSQL_PASSWORDso it stays out of the config file.redis:NewClient(ctx, logger, *config.Config) (*redis.Client, error).traces:New(ctx, serviceName) (shutdown func(context.Context) error, tracer, error),WithSpan(ctx, tracer, name, fn),AddAttributesToCurrentSpan, andGetTraceID(ctx) string.
Running locally
bashmake tools-install # first-time setup make run # API + MySQL + Redis + observability make test # unit tests make test-integration # end-to-end tests against the running stack make lint-all # every linter


