A production-ready Go service template following Clean Architecture principles, designed for microservices with comprehensive infrastructure support.
This project implements Clean Architecture with the following layers:
- Domain Layer (
internal/domain/): Core business entities and rules - Use Case Layer (
internal/usecase/): Application business logic - Infrastructure Layer (
internal/infrastructure/): External concerns (config, logging, repositories) - API Layer (
internal/api/): HTTP handlers and DTOs - Server Layer (
server/): Application setup and routing
- Clean Architecture: Separation of concerns with dependency inversion
- Gin Web Framework: High-performance HTTP web framework
- Structured Logging: Zap-based logging with context propagation
- Redis Integration: Caching and session management
- Docker Support: Containerized deployment with multi-stage builds
- Health Checks: Kubernetes-ready health endpoints
- Comprehensive Testing: Unit and integration test support
- Code Quality: Linting, formatting, and security scanning
The Clean Code Blog by Robert C. Martin (Uncle Bob) - https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html
- Go 1.24.0 or higher
- Docker and Docker Compose
- Make
GVM allows you to easily install and manage multiple Go versions. Here's how to set it up:
On macOS:
# Install GVM
bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
# Add GVM to your shell profile
echo '[[ -s "$HOME/.gvm/scripts/gvm" ]] && source "$HOME/.gvm/scripts/gvm"' >> ~/.zshrc
source ~/.zshrc# Install Go 1.24.0
gvm install go1.24.0
# Use Go 1.24.0 as default
gvm use go1.24.0 --default
# Verify installation
go version# List available Go versions
gvm listall
# Install a specific Go version
gvm install go1.23.0
# Switch to a different Go version
gvm use go1.23.0
# Set a Go version as default
gvm use go1.24.0 --default
# List installed Go versions
gvm list
# Uninstall a Go version
gvm uninstall go1.23.0If you prefer not to use GVM, you can install Go directly:
- Download Go from https://golang.org/dl/
- Follow the installation instructions for your operating system
- Verify installation with
go version
-
Clone the repository
git clone <repository-url> cd go-service-template-clean-architecture
-
Install dependencies
make deps
-
Setup Env Variables
Copy .env.example -> .env Update .env variables to run service on local -
Run the application
make run
The service will be available at http://localhost:8080
-
Build and run with Docker Compose
make compose-up
-
Stop services
make compose-down
GET /healthResponse:
{
"status": "ok",
"service": "go-service-template"
}The application uses environment variables for configuration:
| Variable | Description | Default |
|---|---|---|
HOST |
Server host | 0.0.0.0 |
PORT |
Server port | 8080 |
REDIS_HOST |
Redis host | localhost |
ENV |
Environment | local |
APP_NAME |
Application name | go-service-template |
READ_TIMEOUT |
HTTP read timeout | 60s |
WRITE_TIMEOUT |
HTTP write timeout | 60s |
# Unit tests
make test
# Integration tests
make integration-test
# All tests with coverage
make test && make integration-test# Lint code
make lint
# Format code
make format
# Run all pre-commit checks
make pre-commitThis service includes Testify and Mockery for mocking interfaces.
go install github.com/vektra/mockery/[email protected]
# check version
mockery versionor through homebrew
brew install mockeryTo generate mocks for all the interfaces.
mockeryNote β please refer to the Mockery Documentation for more information and specific configurations.
βββ cmd/ # Application entry point
βββ internal/ # Private application code
β βββ api/ # HTTP handlers and DTOs
β βββ domain/ # Business entities
β βββ infrastructure/ # External dependencies
β βββ usecase/ # Business logic
βββ server/ # Application setup
βββ docker-compose.yml # Local development setup
- Define domain entities in
internal/domain/ - Implement use cases in
internal/usecase/ - Create API handlers in
internal/api/ - Add routes in
server/router/ - Write tests for all layers
# Build image
docker build -t go-service-template .
# Run container
docker run -p 8080:8080 go-service-templateStructured logging with:
- JSON format for production
- Context propagation
- Correlation IDs
- Log levels (DEBUG, INFO, WARN, ERROR)
This service includes OpenTelemetry for distributed tracing and observability.
Add to your .env file:
Run Jaeger for trace visualization:
docker run -d --name jaeger -p 16686:16686 -p 14250:14250 jaegertracing/all-in-one:latestVisit http://localhost:16686 to view traces.
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and linting
- Submit a pull request
This project is licensed under the MIT Licenseβsee the LICENSE file for details.
For support and questions:
- Create an issue in the repository
- Check the documentation
- Review the code examples
Built with β€οΈ using Go and Clean Architecture principles
