When writing or modifying Go code in this project, ALWAYS follow these rules:
- REJECT any changes that result in:
- Souce cod files >500 lines (excluding generated code)
- Functions/methods >100 lines
- If a change would exceed limits, automatically suggest refactoring instead
Before implementing any feature/fix, check:
- Will this create code duplication? (>15 similar lines)
- Does this function do multiple unrelated things? (SRP violation)
- Are variable/function names clear and descriptive?
- Is error handling present with proper context?
// DON'T
if err != nil {
return err
}
// DO
if err != nil {
return fmt.Errorf("failed to process user %s: %w", userID, err)
}// Early returns, flat structure
func ProcessOrder(order Order) error {
if err := order.Validate(); err != nil {
return fmt.Errorf("invalid order: %w", err)
}
if order.Total == 0 {
return ErrEmptyOrder
}
// main logic here
return nil
}- Packages: lowercase, single word (e.g.,
user,auth, notuserService) - Interfaces:
<Noun>erpattern (e.g.,Reader,Handler) or<Noun>Service - Exported types: Clear, descriptive (e.g.,
UserRepository, notUsrRepo) - Private helpers: verb-first (e.g.,
buildQuery,validateInput)
internal/
├── domain/ # Business entities, interfaces
├── handler/ # RESTFul handlers (thin entrypoints)
├── service/ # Business logic
├── repository/ # Data access
└── pkg/ # Shared utilities
Rule: Handlers should be <50 lines. If longer, extract to service layer.
Before writing, ask yourself:
- Does this belong in a new file/package? (cohesion)
- Can I extract common logic? (DRY)
- Is this testable? (dependency injection)
After writing, verify:
- Run
go fmt,go test ./... - Check function/file line counts
- Add godoc comments for exported items
If you detect these patterns, proactively suggest refactoring:
- Function >80 lines (approaching limit)
- Copy-pasted code blocks
- Deeply nested conditions (>3 levels)
- Missing error handling
- Global variables for application state
When modifying existing code that doesn't meet standards:
- Fix the immediate area you're working on
- Suggest follow-up refactoring for the whole file
- Don't rewrite unrelated code without asking