A modular, scalable Go codebase template built with Clean Architecture principles.
Go version: v1.25
This is a production-ready template for building Go applications with:
- β Clean separation of concerns
- β Framework-independent business logic
- β Fully testable code
- β Easy to extend and maintain
π Learn more about Clean Architecture
The easiest way to create a new project based on this architecture:
# Install the generator
go install github.com/dung13890/go-base-gen@latest
# Generate your new project
go-base-gen project --pkg github.com/yourusername/yourproject --path yourproject
# Setup and run
cd yourproject
go mod tidy
cp .env.example .env
go run cmd/migrate/main.go
make devπ View go-base-gen documentation
To explore or contribute to the template itself:
# Clone the repository
git clone https://github.com/dung13890/go-clean-architecture.git
cd go-clean-architecture
git submodule update --init --force --remote
# Build and run with Docker
docker compose build
cp .env.example .env
docker compose up -d
# Access the container
docker compose exec go-app sh
# Inside container: Setup database
make create_example_table.sql
go run cmd/migrate/main.go
go run cmd/seed/main.go
# Run the application
air -c cmd/app/.air.tomlHandler β UseCase β Domain Interface (gateway, Repository, internal service)
β β
Validation ββ Adapters (Cache, DB, External)
internal/
βββ domain/ # π― Core Business Layer (Layer 1)
β βββ entity/ # Business entities (User, Role, etc.)
β βββ repository/ # Repository interfaces
β βββ gateway/ # Gateway interfaces
β βββ service/ # Pure domain business services
β
βββ usecase/ # π Application orchestrates workflow (Layer 2)
βββ adapter/ # π External Integrations (Layer 3)
β βββ repository/ # Data persistence
β βββ gateway/ # External services (SMTP, Cache)
β βββ presenter/ # maps usecase output -> delivery
β
βββ delivery/ # π User Interface Layer (Layer 4)
β βββ http/ # HTTP handlers & routes
βββ infrastructure/ # βοΈ Cross-cutting Concerns (Layer 4)
βββ registry/ # ποΈ Dependency Injection
| Layer | Responsibility | Example |
|---|---|---|
| Domain | Core business rules & interfaces | User entity, UserRepo |
| Usecase | Application workflows & orchestration | Register user flow |
| Adapter | External system integration | PostgreSQL, Redis, SMTP |
| Delivery | Request/response handling | HTTP handlers, CLI |
| Infrastructure | Technical concerns | Config, DB, Redis |
- π JWT Authentication β Secure token-based auth
- π€ User Management β Full CRUD operations
- π Role & Permissions β Access control system
- π§ Email Service β SMTP integration
- π¦ Rate Limiting β Redis-based throttling
- π Hot Reload β Development with Air
- π§ͺ Testing Ready β Mock generation included
# Code quality
make lint # Run linter
make test # Run unit tests
make go-gen # Generate mocks
# Database
make create_example_table.sql # Create migration file
go run cmd/migrate/main.go # Run migrations
go run cmd/migrate/main.go down 1 # Rollback 1 step
go run cmd/seed/main.go # Seed database
# Development
make dev # Run with hot reloadcurl -X POST 'http://localhost:8080/api/register' \
-H 'Content-Type: application/json' \
-d '{
"email": "user@example.com",
"password": "password",
"role_id": 1,
"name": "John Doe"
}'curl -X POST 'http://localhost:8080/api/login' \
-H 'Content-Type: application/json' \
-d '{
"email": "user@example.com",
"password": "password"
}'| Category | Technology |
|---|---|
| Framework | Echo |
| ORM | GORM |
| Configuration | Viper |
| Authentication | JWT |
| Hot Reload | Air |
| Database | PostgreSQL |
| Cache | Redis |
Want to add a new domain (e.g., "Product")?
# Use the generator tool
go-base-gen domain --dn product --pkg github.com/yourusername/yourproject
# This creates:
# - internal/domain/entity/product.go
# - internal/domain/repository/product_repo.go
# - internal/usecase/product/
# - internal/adapter/repository/product_dao.go
# - internal/adapter/repository/product_repo.go
# - internal/delivery/http/product_handler.go- Dependency Rule: Inner layers don't know about outer layers
- Interface Segregation: Use small, focused interfaces
- Dependency Injection: Wire dependencies in registry
- Testability: Mock external dependencies
Contributions are welcome! Please feel free to submit a Pull Request.
If you find this project helpful:
MIT License - see LICENSE for details.
