How elct9620/clean-architecture-in-go-2025 works
This project is a cleanly structured example of Clean Architecture implemented in Go. It’s designed as a sample ordering system that supports both gRPC and HTTP interfaces. The architecture follows the classic separation of concerns by dividing the application into multiple layers such as API (delivery), use cases (business logic), domain entities, repository (data access), and even includes separate entry points for different protocols.
Main Components
• Entry Points (cmd):
• Two main executables: one for gRPC (cmd/grpc) and one for HTTP (cmd/http). Each has its own main.go file and uses dependency injection (via Wire) to bootstrap the application.
• API Layer (internal/api):
• gRPC API (internal/api/grpc) that exposes endpoints like LookupOrder and PlaceOrder.
• REST API (internal/api/rest) that similarly provides equivalent endpoints and includes configuration such as an OpenAPI specification.
• Domain (internal/entity):
• Domain entities encapsulate business data, for example, orders and tokens, with separate subdirectories.
• Use Cases (internal/usecase):
• Contains the business logic around order lookup and placement, integrating with the other layers via repository interfaces.
• Repository Layer (internal/repository):
• Contains multiple implementations including in-memory, bolt (key-value store), LRU caching, and SQLite. This abstraction allows flexibility in choosing the data source.
• Validator (internal/validator):
• Provides domain-specific validation logic.
• Database Scripts (db):
• SQL scripts (schema, orders, tokens) that define the database schema and sample data for persistence.
• Auxiliary/Testing Components:
• Feature files in the “features” directory imply behavior-driven testing.
• Files like godogs_test.go and http_test.go for testing.
• The “internal/testability” directory suggests infrastructure to facilitate integration or acceptance testing.
• Protocol Buffers (pkg/orderspb):
• Contains protobuf definitions and generated code for gRPC communications.
• Dependency Injection:
• Use of Wire (wire.go and wire_gen.go in various directories) indicates that dependency injection is employed to configure and wire up components.
• Configuration & Tooling:
• Files such as sqlc.yaml, Makefile, and GitHub workflows indicate CI/CD and code generation steps supporting the project lifecycle.
Relationships and Interactions
• The entry points (gRPC and HTTP main functions) initialize the respective API servers.
• The API layer (both gRPC and REST) receives client requests and passes them to the use case layer, which encapsulates the core business logic.
• The use cases interact with the repository layer to persist and retrieve data from either in-memory databases, SQLite, or other storage implementations.
• The entities represent the fundamental domain models (orders, tokens) used throughout the business logic and persistence layers.
• The validator is applied before the use cases perform operations to ensure that incoming data meets business requirements.
• External interfaces (API) communicate via clearly defined protocol contracts, with gRPC using the protocol buffers defined in pkg/orderspb, and REST exposing an OpenAPI-defined interface.
Architectural Patterns and Design Principles
• Clean Architecture: Separation of concerns, with distinct layers for delivery, business logic, domain entities, and repository abstraction.
• Dependency Injection: Configured via Wire, ensuring that dependencies are managed and injected in a centralized manner.
• Protocol Abstraction: The API layer is abstracted in such a way that both gRPC and REST interfaces can use the same underlying business logic.
• Extensibility: Multiple repository implementations signal that the application can run in different environments (testing, production) with minimal changes.
Relevant Technologies & Frameworks
• Go as the main language.
• gRPC and Protocol Buffers for one of the communication methods.
• REST/HTTP with OpenAPI for API documentation and client communication.
• Wire for dependency injection.
• Various storage backends (SQLite, BoltDB, in-memory solutions).
• CI tools via GitHub workflows.
• Makefile and SQL scripts to manage builds, tests, and database initialization.
Elements
• Use clear labels for all major components (e.g., “gRPC API”, “REST API”, “Business Logic/Use Cases”, “Repositories”, “Domain Entities”, “Database”).
• From API (gRPC/REST) to Use Cases – indicating incoming request processing.
• From Use Cases to Repositories – indicating data access.
• From Repositories to Database – showing persistent storage interactions.