CUG Prompt Instructions is a comprehensive guide for implementing Clean Architecture in Golang application development. This guide provides clear structure, patterns, and rules for building modular, testable, and maintainable systems.
This repository contains a series of interconnected guidance documents:
- 00-main-rules.md - Main rules and basic terminology.
- 01-core-components.md - Core component definitions: ActionHandler, Gateway, and Usecase.
- 02-usecase-test.md - Unit testing guidelines for usecases.
- 03-controller.md - Controller implementation for HTTP, scheduler, and message subscribers.
- 04-middleware.md - Middleware for transactions, logging, and retry.
- 05-wiring.md - Dependency injection and component composition.
├── controller/ # Exposes usecases to the outside world
├── core/ # Contains ActionHandler definition
├── gateway/ # External interfaces and system utilities
├── middleware/ # Cross-cutting concerns (transaction, logging, retry)
├── model/ # Domain entities
├── usecase/ # Business logic
├── utils/ # Helper functions
└── wiring/ # Connects components
- Controller: Exposes usecases via protocols (HTTP, MQTT, schedulers, etc.)
- Usecase: Pure business logic, orchestrates gateways, no infrastructure dependencies
- Gateway: Handles infrastructure and non-deterministic functions
- Middleware: Decorators for cross-cutting concerns (transactions, logging, etc.)
- Wiring: Dependency injection and component composition
All components (usecases and gateways) implement a consistent ActionHandler pattern:
type ActionHandler[REQUEST any, RESPONSE any] func(ctx context.Context, request REQUEST) (*RESPONSE, error)- Analyze requirements thoroughly
- Read core documentation
- Scan existing code
- Plan implementation strategy
- Check module imports
- Implement components
- Implement unit tests
- Run code validation
- Contain pure business logic
- Never access infrastructure directly
- Use gateways for infrastructure operations
- Implement input validation
- Follow standard business logic flow
- Handle infrastructure operations
- One task per gateway
- Use transaction middleware for DB operations
- Wrap errors with context
- No business logic
- One controller calls exactly one usecase
- No business logic
- Handle protocol-specific concerns
- Implement proper error handling
- Implement decorator pattern
- Add cross-cutting functionality
- Apply middleware in the correct order
- Initialize all components
- Inject dependencies
- Apply middleware
- Register controllers
- Test each usecase in isolation
- Mock all gateway dependencies
- Test both success and error paths
- Structure tests consistently
- Use table-driven approach
- Prioritize component reuse
- Follow consistent naming conventions
- Maintain strict separation between usecase and gateway
- Avoid dependency cycles
- No global state sharing
- Make usecase functionality pure functions
- Manage database transactions carefully
Before implementation, ensure you thoroughly understand these guidelines and follow the mandatory development workflow outlined in 00-main-rules.md.