This project is a starter template for building scalable, maintainable, and testable applications using the Clean Architecture pattern in Go. The clean architecture focuses on organizing your code into layers, separating concerns to improve the maintainability and testability of the codebase.
The project is structured in a way that separates business logic, domain models, and infrastructure concerns. Below is a breakdown of the directory structure:
golang-clean-architecture/
├── cmd/ # Entry point of the application
│ └── main.go # Main application file
├── config/ # Configuration settings (e.g., environment variables, config files)
│ └── config.go # Config loader
├── conn/ # Database and service connections
│ └── db.go # Database connection setup
├── controllers/ # API controllers for handling HTTP requests
│ └── user_controller.go
├── domain/ # Domain models and business logic
│ └── user.go
├── middlewares/ # Custom middleware (e.g., authentication, logging)
│ └── auth_middleware.go
├── repositories/ # Repositories for interacting with databases or external services
│ └── user_repository.go
├── routes/ # Routing definitions for the web application
│ └── routes.go
├── services/ # Application services implementing business logic
│ └── user_service.go
├── utils/ # Utility functions and helpers
│ └── hash.go
├── worker/ # Background job processing
│ └── job_worker.go
├── Dockerfile # Docker setup
├── docker-compose.yml # Docker Compose configuration for multi-container setups
├── go.mod # Go modules file
├── go.sum # Go modules checksum file
├── Makefile # Task runner for automating common tasks
└── README.md # Project documentation
-
cmd/: The main application entry point. This file is responsible for starting the server and wiring up all the layers of the application.
-
config/: Centralized configuration management. Stores application settings and environment variables.
-
conn/: Database and external service connections. This is where you configure and manage connections like PostgreSQL, MySQL, Redis, or third-party APIs.
-
controllers/: Contains API controllers that handle incoming HTTP requests and delegate them to appropriate services.
-
domain/: Contains the core domain models and business logic interfaces. This layer should remain independent of external services or frameworks.
-
middlewares/: Custom middleware functions for request handling, such as logging, authentication, and request validation.
-
repositories/: Implements the repository pattern for interacting with databases or external services. It abstracts the data source from the rest of the application.
-
routes/: Defines the routing for the API, mapping HTTP routes to controllers.
-
services/: Implements the application’s business logic by coordinating between controllers, repositories, and other services.
-
utils/: Contains helper functions and utilities that are used across the application (e.g., password hashing, string manipulation).
-
worker/: Implements background job processing for tasks that don’t need to block the request-response cycle (e.g., sending emails, processing large data sets).
- Go (1.19+)
- Docker
- PostgreSQL or any database of your choice
-
Clone the repository:
git clone https://github.com/rakib-09/golang-clean-architecture.git cd golang-clean-architecture
-
Build and run the application using Docker:
docker-compose up --build
-
Access the application at
http://localhost:8080
.
You can run the tests using the following command:
go test ./...
The Makefile
contains predefined commands to simplify your workflow:
make build
: Build the application.make run
: Run the application locally.make test
: Run the test suite.make clean
: Clean the project build files.
- Go: The programming language used to build the application.
- Docker: For containerization and development environment setup.
- PostgreSQL: Default database, can be replaced by any other DBMS.
- Asynq: For background task processing.
- log/slog: Used for structured logging.
Feel free to submit issues, fork the repository, and open pull requests for improvements or bug fixes.
This `README.md` provides a detailed overview of your project’s architecture, setup, and usage. Let me know if you'd like to make any adjustments!