How bxcodec/go-clean-arch works
Project Type & Purpose
• This project is an example of implementing Clean Architecture in Go. It’s structured around domain-driven design with a clear separation between the delivery (interface) layer, use case/service layer, repository layer, and domain (entity) layer.
• The README outlines that the project follows Uncle Bob’s Clean Architecture principles, making it all about decoupling business rules from frameworks, UI, database, and external agencies.
Analyzing the File Structure
• Top-Level Directories and Files:
• "app/main.go": The application’s entry point.
• "domain/": Contains domain entities (e.g., article.go, author.go, errors.go). These define core business objects.
• "article/": Contains an example use case related to articles, including a service file and mock implementations.
• "internal/":
▪ "internal/rest/": Contains the REST delivery layer (e.g., article.go for HTTP endpoints and middleware for CORS and timeouts). This represents the outward-facing API.
▪ "internal/repository/": Houses repository implementations, especially under "internal/repository/mysql" which shows the concrete data storage (MySQL) implementations and tests.
▪ "internal/workers/": Space reserved for background jobs or asynchronous tasks.
• "misc/": Contains supporting Makefiles and tooling instructions.
• Configuration & Deployment Files:
• Dockerfile, compose.yaml, example.env, and .air.toml point to containerization, hot reloading, and environment configuration details.
Directions
a. Main Components:
• Entry Point: The "app" directory (main.go) acts as the starting point.
• Delivery/Interface Layer: Represented by “internal/rest” which handles HTTP requests and middleware.
• Use Case/Service Layer: Found in "article/service.go" and generally tied to executing business logic.
• Domain Model: The "domain" package encapsulates business entities such as Article and Author.
• Repository/Data Access Layer: "internal/repository/mysql" manages database interactions (with a corresponding SQL file, article.sql, to set up the database schema).
• Background Processing: "internal/workers" which may handle asynchronous tasks.
• Supporting Infrastructure: Tools such as Docker (Dockerfile, compose.yaml), testing (Makefile, mocks in both "article/mocks" and "internal/rest/mocks"), and CI (the GitHub workflows).
b. Relationships and Interactions:
• The Delivery Layer (REST APIs/controllers) receives external HTTP calls and invokes the Use Case/Service Layer.
• The Use Case/Service Layer coordinates with the Domain Model (business entities) and calls into the Repository Layer for data storage/retrieval from MySQL.
• Bidirectional interactions where needed (e.g., responses coming back up the chain).
• “Workers” as an independent component that interacts with core services, especially if there’s asynchronous processing.
c. Architectural Patterns/Design Principles:
• Clean Architecture layers (Domain, Usecase, Delivery, Repository) must be clearly delineated.
• Dependency inversion: Higher-level modules (services, controllers) depend on abstractions (interfaces defined within the domain or service layer) rather than concrete implementations. For example, repository interfaces might be implemented in the MySQL sub-package.
• How tests and mock implementations (present in the "mocks" directories) allow for isolation of components.
d. Relevant Technologies/Frameworks:
• Programming Language: Go (Golang)
• Frameworks/Libs: Interfaces for REST controllers, middleware (CORS, timeout), and repository pattern usage.
• Database: MySQL (as indicated by article.sql and repository/mysql)
• DevOps Tools: Docker (Dockerfile, compose.yaml), CI workflows (GitHub Actions in .github/workflows), Makefiles for task automation.