Opinionated Domain Driven Design Template for Go
go-ddd jump-starts production-grade Go backends that keep business rules, infrastructure, and delivery code cleanly separated. Out of the box you get opinionated DDD building blocks, CQRS command and query flows, idempotent write paths, domain events with a transactional outbox, and tooling to keep schema and code in lockstep.
New to DDD? Start with the tutorial → A nine-chapter walkthrough that teaches Domain-Driven Design from zero, using this codebase as the running example.
migrate.go, and sqlc make schema evolution explicit and reproducible.Money value object, cross-module interactions, and validation rules.internal/ for domain, application, infrastructure, interface, plus testhelpers for fixture reuse.cmd/marketplace/main.go ready to wire adapters or frameworks of your choice.migrations/ and sql/ plus generated data access via sqlc./healthz + /readyz probes, and a one-command Docker Compose stack.sqlc for type-safe PostgreSQL access.Makefile, Dockerfile, and docker-compose.yml for a one-command local stack.Domain-Driven Design connects implementation to an evolving model. go-ddd showcases this by modelling a simple marketplace where Sellers manage Products, exercising aggregates, value objects, and validation flows.
Anatomy of a write request:
…DDD from zero: the full tutorial — nine chapters from "why DDD" to entities, value objects, aggregates, repositories, CQRS, the outbox, idempotency, and testing, all anchored to this codebase.
Comprehensive DDD & CQRS Principles Guide - Learn how to apply these patterns to any business domain.
domain: The heart of the software, representing business logic and rules.entities: Fundamental objects within our system, like Product and Seller. Contains basic validation logic.application: Contains use-case specific operations that interact with the domain layer.infrastructure: Supports the higher layers with technical capabilities like database access.db: Database access and models.repositories: Concrete implementations of our storage needs.interface: The external layer which interacts with the outside world, like API endpoints.api/rest: Handlers or controllers for managing HTTP requests and responses.find vs get:find methods can return null or an empty list.get methods must return a value. If the value is not found, throw an error.deleted_at column in your database and set it to the current timestamp when deleting an entity. This way, you can always restore the entity if needed.CQRS separates read operations (queries) from write operations (commands) in your application. In this codebase:
This separation enables different optimization strategies:
Idempotency ensures that multiple identical requests have the same effect as a single request. This is crucial for handling network failures and retries in distributed systems. Implementation:
Idempotency-Key HTTP header is preferred; an idempotency_key field in the JSON body is still honored as a fallback, and the header wins when both are sentINSERT ... ON CONFLICT DO NOTHING), so two concurrent requests with the same key can never both execute — no check-then-write raceThis prevents duplicate entities from being created when clients retry failed requests.
Aggregates record events (e.g. ProductCreated) when something business-relevant happens. Instead of publishing them directly to a broker — which risks losing events when the process crashes between the DB commit and the publish — events are stored in an outbox_events table. A relay polls the outbox and publishes unpublished events with at-least-once delivery. See internal/domain/events/ and internal/infrastructure/outbox/.
This project uses golang-migrate for database schema management. Migrations are stored in the migrations/ directory with sequential version numbers.
migrations/
├── 000001_initial_schema.up.sql # Creates initial tables
├── 000001_initial_schema.down.sql # Rollback for initial schema
├── 000002_price_as_money.up.sql # Money as integer cents + currency
├── 000003_outbox.up.sql # Transactional outbox table
├── 000004_price_minor_units.up.sql # Rename to ISO 4217 minor units
└── ...# Apply all pending migrations
go run migrate.go -database-url "postgres://user:pass@localhost/db?sslmode=disable" -command up
# Rollback last migration
go run migrate.go -database-url "postgres://user:pass@localhost/db?sslmode=disable" -command down -steps 1
# Check current version
go run migrate.go -database-url "postgres://user:pass@localhost/db?sslmode=disable" -command version
# Force to specific version (use with caution)
go run migrate.go -database-url "postgres://user:pass@localhost/db?sslmode=disable" -command force -version 1# Install the CLI tool
go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
# Apply all pending migrations
migrate -path migrations -database "postgres://user:pass@localhost/db?sslmode=disable" up
# Rollback last migration
migrate -path migrations -database "postgres://user:pass@localhost/db?sslmode=disable" down 1# Create a new migration
migrate create -ext sql -dir migrations -seq add_user_email_columnThis will create two files:
0000NN_add_user_email_column.up.sql - Forward migration0000NN_add_user_email_column.down.sql - Rollback migrationup and down migrationsRequires Go 1.26+. Run
make helpto see all available targets.
Bring up Postgres, apply migrations, and start the API in one command:
make docker-up # docker compose up --build
# API is now available on http://localhost:8080
make docker-down # tear everything downCreate a seller:
curl -s -X POST http://localhost:8080/api/v1/sellers \
-H 'Content-Type: application/json' \
-d '{"name": "Acme Corp", "idempotency_key": "create-acme-1"}'{"id":"0197a3c2-...","name":"Acme Corp","created_at":"2026-07-14T09:00:00Z","updated_at":"2026-07-14T09:00:00Z"}Create a product for that seller (prices are integer minor units — never floats):
curl -s -X POST http://localhost:8080/api/v1/products \
-H 'Content-Type: application/json' \
-d '{"name": "Wooden Chair", "price_minor_units": 4999, "currency": "EUR", "seller_id"No open issues yet, or sync has not completed.