1---2name: golang3description: Provides idiomatic Go programming expertise and best practices. Ensures clean, efficient, and maintainable code following official Go conventions. Specializes in concurrent programming patterns, interface design, error handling strategies, and performance optimization. Masters standard library usage and ecosystem integration. Use when: writing Go code (.go files), designing interfaces and struct types, implementing concurrent patterns (goroutines/channels), handling errors idiomatically, writing table-driven tests, creating Go modules, optimizing performance-critical code, managing dependencies with go.mod, implementing HTTP servers and clients, working with context propagation, or designing package APIs for public libraries.4---5
6# Go Coding Standards
7
8## Basic Principles
9
10### One Function, One Responsibility
11
12- If function name connects with "and" or "or", it's a signal to split
13- If test cases are needed for each if branch, it's a signal to split
14
15### Conditional and Loop Depth Limited to 2 Levels
16
17- Minimize depth using early return whenever possible
18- If still heavy, extract into separate functions
19
20### Make Function Side Effects Explicit
21
22- Example: If `getUser` also runs `updateLastAccess()`, specify it in the function name
23
24### Convert Magic Numbers/Strings to Constants When Possible
25
26- Declare at the top of the file where used
27- Consider separating into a constants file if there are many
28
29### Function Order by Call Order
30
31- Follow Go's clear conventions if they exist
32- Otherwise, order top-to-bottom for easy reading by call order
33
34### Review External Libraries for Complex Implementations
35
36- When logic is complex and tests become bloated
37- If industry-standard libraries exist, use them
38- When security, accuracy, or performance optimization is critical
39- When platform compatibility or edge cases are numerous
40
41### Modularization (Prevent Code Duplication and Pattern Repetition)
42
43- Absolutely forbid code repetition
44- Modularize similar patterns into reusable forms
45- Allow pre-modularization if reuse is confirmed
46- Avoid excessive abstraction
47- Modularization levels:
48 - Same file: Extract into separate function
49 - Multiple files: Separate into different package
50 - Multiple projects/domains: Separate into different module
51
52### Variable and Function Names
53
54- Clear purpose while being concise
55- Forbid abbreviations outside industry standards (id, api, db, err, etc.)
56- Don't repeat context from the parent scope
57- Boolean variables use `is`, `has`, `should` prefixes
58- Function names are verbs or verb+noun forms
59- Plural rules:
60 - Pure arrays/slices: "s" suffix (`users`)
61 - Wrapped struct: "list" suffix (`userList`)
62 - Specific data structure: Explicit (`userSet`, `userMap`)
63 - Already plural words: Use as-is
64
65### Field Order
66
67- Alphabetically ascending by default
68- Maintain consistency in usage
69
70### Error Handling
71
72- Error handling level: Handle where meaningful response is possible
73- Error messages: Technical details for logs, actionable guidance for users
74- Error classification: Distinguish between expected and unexpected errors
75- Error propagation: Add context when propagating up the call stack
76- Recovery vs. fast fail: Recover from expected errors with fallback
77- Use %w for error chains, %v for simple logging
78- Wrap internal errors not to be exposed with %v
79- Never ignore return errors from functions; handle them explicitly
80- Sentinel errors: For expected conditions that callers must handle, use `var ErrNotFound = errors.New("not found")`
81
82## File Structure
83
84### Element Order in File
85
861. package declaration
872. import statements (grouped)
883. Constant definitions (const)
894. Variable definitions (var)
905. Type/Interface/Struct definitions
916. Constructor functions (New\*)
927. Methods (grouped by receiver type, alphabetically ordered)
938. Helper functions (alphabetically ordered)
94
95## Interfaces and Structs
96
97### Interface Definition Location
98
99- Define interfaces in the package that uses them (Accept interfaces, return structs)
100- Only separate shared interfaces used by multiple packages
101
102### Pointer Receiver Rules
103
104- Use pointer receivers for state modification, large structs (3+ fields), or when consistency is needed
105- Use value receivers otherwise
106
107## Context Usage
108
109### Context Parameter
110
111- Always pass as the first parameter
112- Use `context.Background()` only in main and tests
113
114## Testing
115
116### Testing Libraries
117
118- Prefer standard library's if + t.Errorf over assertion libraries like testify
119- Prefer manual mocking over gomock
120
121## Forbidden Practices
122
123### init() Functions
124
125- Generally forbidden. Prefer explicit initialization functions
126
127## Package Structure
128
129### internal Package
130
131- Actively use for libraries, use only when necessary for applications
132
133## Recommended Libraries
134
135- Web: Fiber
136- DB: Bun, SQLBoiler (when managing migrations externally)
137- Logging: slog
138- CLI: cobra
139- Utilities: samber/lo, golang.org/x/sync
140- Configuration: koanf (viper if cobra integration needed)
141- Validation: go-playground/validator/v10
142- Scheduling: github.com/go-co-op/gocron
143- Image processing: github.com/h2non/bimg