Clean Architecture Go backend template for building maintainable, testable APIs with built-in caching and observability, designed for engineers and AI agents.
For both engineers and AI agents. Shared rules in AGENTS.md keep structure consistent. The template:
- Provides a reusable backend starter that follows clean architecture.
- Keeps business logic isolated from frameworks and vendors.
- Offers production-ready foundations: HTTP, DB (PostgreSQL with pgvector), cache-aside user store, KV-backed auth (session/OTP/OAuth), migrations, tracing, metrics, and structured logging.
- Serves as a base repo to clone for new app projects.
Architecture & structure
- Clean Architecture
- Dependency Injection (Composition root)
- SOLID principles
- Consumer-owned interfaces
Structural patterns
Behavioral & creational
Data & transport
See package-level README.md files and AGENTS.md for implementation guidance and shared architecture rules for both engineers and AI agents.
Transport
Echo v5- HTTP servergo-playground/validator/v10- request/DTO validation (struct tags)OpenAPI 3- source of truth for HTTP contractsoapi-codegen- backend transport model generation from OpenAPI
Database
pgx/v5- PostgreSQL driver and connection poolsqlc- static SQL query generationMasterminds/squirrel- dynamic SQL constructionpressly/goose- database migrations
Why SQL-first data access (no ORM)
- Raw SQL with
sqlc+squirrelprovides explicit query control, predictable performance tuning, and compile-time type safety. - Common downsides are handled by:
sqlcgenerated typed mappings to reduce runtime schema/query mismatch risk.squirrelcomposable dynamic SQL to avoid fragile string concatenation.- Clean Architecture + repository boundaries to isolate SQL in infra adapters and keep usecases storage-agnostic.
Cache
go-redis/v9- Redis client integration
Observability & logging
uber-go/zap- structured loggingOpenTelemetrySDK +OTLPexporters - tracing, logs, and metricsHyperDX+OpenTelemetry Collector- local observability integration
Development & infra
air- local hot reloadDocker Compose- local infrastructure orchestration
- Create a new repository from this template.
- Bootstrap the project:
./scripts/bootstrap-template.sh --module github.com/your-org/your-backendThis updates module/import paths, service and stack naming, OpenAPI title, and README title.
- If you cloned this repo directly, rename your project directory and set the Git remote to your new repository. The script does not change directory names or remotes.
- Validate with
make openapi-generate && make test. - Review
docker-compose.yml,.env.example, anddocs/openapi.yamlfor project-specific values. The auth (pluggable OTP/OAuth + session), cached user store, and health modules are production-ready foundations—extend them and add your own migrations and features. - Review
AGENTS.mdand package-levelREADME.mdfiles before feature development.
cp .env.example .envmake installmake dev-up && make migrate-upmake run- Verify
GET /health?check=ready
Common commands: make dev-logs, make dev-down, make migrate-status, make openapi-generate, make run-stop.
Default local ports: Postgres 5432, Redis 6379, OTel 4317/4318, HyperDX 8081.