Go Hexagonal Architecture
A Simple RESTful Service API written in Go implementing Hexagonal Architecture (Ports and Adapters pattern) using modern Go technologies and best practices.

ποΈ Architecture Overview
This project demonstrates the implementation of Hexagonal Architecture in Go, also known as the Ports and Adapters pattern. The architecture provides a clear separation of concerns and makes the application highly testable and maintainable.
Key Principles
- Domain-Driven Design (DDD): Business logic is isolated in the appliation layer
- Dependency Inversion: High-level modules don't depend on low-level modules
- Testability: Each layer can be tested in isolation
- Flexibility: Easy to swap implementations (e.g., database, web framework)
π οΈ Tech Stack
- Language: Go 1.21+
- Web Framework: Gin - Fast HTTP web framework
- Database: PostgreSQL with native driver
- Cache: Redis for caching and session management
- Authentication: PASETO - Secure token-based authentication
- Configuration: Environment variables with structured config
- Logging: Structured logging with slog
- API Documentation: Swagger with gin-swagger
- Validation: Request validation with go-playground/validator
π Project Structure
go-hexagonal-architecture/
βββ cmd/
β βββ http/
β βββ main.go # Application entry point
βββ config/ # Config for application
βββ docs/
β βββ architecture.webp # Architecture diagram
β βββ db.webp # Database schema
βββ infrastructure/
β β βββ storage/ # Infrastructure storage
β β β βββ postgres/ # Infrastructure postgres
β β β βββ redis/ # Infrastructure redis
βββ internal/
β βββ application/
β β βββ domain/ # Domain entities and business rules
β β β βββ auth/ # Domain auth
β β β βββ category/ # Domain category
β β β βββ order/ # Domain order
β β β βββ payment/ # Domain payment
β β β βββ product/ # Domain product
β β β βββ user/ # Domain user
β β β βββ error.go # Domain error
β β βββ mock/ # Application mock ports/interfaces
β β βββ port/ # Application ports/interfaces
β β βββ util/ # Application util
β β βββ usecase/ # Application services (use cases)
β βββ adapter/
β β βββ auth/paseto/ # Auth (primary adapter)
β β βββ handler/http/ # HTTP handlers (primary adapter)
β β βββ logger/ # Logger util for application
β β βββ redis/ # Secondary adapater for redis
β β βββ repository/ # Secondary adapater for postgres
β β β βββ model/ # Model in repository
β β β βββ *.go # Repository implementation
βββ docker-compose.yml # Local development setup
βββ Dockerfile # Container image
βββ .env.example # Environment variables template
βββ go.mod # Go modules
βββ go.sum # Go modules checksums
βββ LICENSE
βββ Makefile
βββ README.md # This file
π Getting Started
Prerequisites
- Go 1.21 or higher
- Docker and Docker Compose (for local development)
- PostgreSQL (if running without Docker)
- Redis (if running without Docker)
Local Development with Docker
-
Clone the repository
git clone https://github.com/TienMinh25/go-hexagonal-architecture.git
cd go-hexagonal-architecture
-
Set up environment variables
cp .env.example .env
# Edit .env with your configuration
-
Start services with Docker Compose
docker-compose up -d
-
Run the application
go run cmd/server/main.go
π Database Schema

The database schema includes the core entities and their relationships, designed following DDD principles.
π API Endpoints
Users
GET /api/v1/users # Get all users
GET /api/v1/users/:id # Get user by ID
POST /api/v1/users # Create new user
PUT /api/v1/users/:id # Update user
DELETE /api/v1/users/:id # Delete user
Example Request
# Create a new user
curl -X POST http://localhost:8080/api/v1/users \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "john@example.com"}'
π§ͺ Testing
The hexagonal architecture makes testing straightforward by allowing each layer to be tested independently.
# Run all tests
go test ./...
# Run tests with coverage
go test -cover ./...
Test Structure
- Unit Tests: Test individual components in isolation
ποΈ Architecture Layers
1. Domain Layer (Core)
Contains the business entities and rules. This layer has no dependencies on external frameworks or libraries.
- Entities: Core business objects
- Value Objects: Immutable objects that represent concepts
- Domain Services: Business logic that doesn't belong to a single entity
2. Application Layer (Core)
Contains application-specific business logic and use cases.
- Services: Application services that orchestrate domain operations
- Ports: Interfaces that define contracts with the outside world
3. Adapter Layer
Implements the ports defined in the application layer.
- Primary Adapters: Handle incoming requests (HTTP handlers)
- Secondary Adapters: Handle outgoing requests (database, external APIs)
4. Infrastructure Layer
Provides technical capabilities that support the higher layers.
- Configuration: Application configuration management
- Database: Database connection and setup
- Logging: Application logging setup
π³ Docker Support
Build Docker Image
docker build -t go-hexagonal-architecture .
Run with Docker Compose
docker-compose up --build
The docker-compose.yml includes:
- Application container
- PostgreSQL database
- Redis cache
- pgAdmin (database management)
βοΈ Configuration
Environment variables can be set in .env file or as system environment variables:
# Server Configuration
PORT=8080
GIN_MODE=release
# Database Configuration
DATABASE_URL=postgres://username:password@localhost:5432/dbname?sslmode=disable
DB_MAX_OPEN_CONNS=25
DB_MAX_IDLE_CONNS=25
DB_MAX_IDLE_TIME=15m
# Redis Configuration
REDIS_URL=redis://localhost:6379
REDIS_PASSWORD=
REDIS_DB=0
# Logging
LOG_LEVEL=info
LOG_FORMAT=json
π Deployment
Production Build
# Build optimized binary
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app cmd/server/main.go
# Or use Docker
docker build -t go-hexagonal-architecture:latest .
Deployment Options
- Container Orchestration: Kubernetes, Docker Swarm
- Cloud Platforms: AWS ECS, Google Cloud Run, Azure Container Instances
- Traditional Servers: Linux VPS with reverse proxy (nginx)
π€ Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature)
- Commit your changes (
git commit -m 'Add some amazing feature')
- Push to the branch (
git push origin feature/amazing-feature)
- Open a Pull Request
Code Style
- Follow Go conventions and best practices
- Use
gofmt for formatting
- Run
golint and go vet before committing
- Write tests for new features
π License
This project is licensed under the Apache License - see the LICENSE file for details.
π Acknowledgments
π Additional Resources
Built with β€οΈ using Go and Hexagonal Architecture principles