Go Coding Standards
Apply these standards when writing or reviewing Go code in this project.
Quick Reference
| Principle | Rule |
|---|---|
| Interfaces | Accept interfaces, return concrete types |
| Errors | Errors are values — handle explicitly with context wrapping |
| Functions | Keep under 50 lines, use early returns |
| Receivers | Be consistent — all pointer or all value |
| Zero values | Make them useful — types should work without initialization |
Core Go Idioms
- Errors are values — No exceptions, explicit error handling
- Make zero values useful — Design types to work without initialization
- Accept interfaces, return concrete types
- Composition over inheritance — Use embedding and interfaces
- Small interfaces — One or two methods per interface
- Early returns — Reduce nesting with guard clauses
Error Handling Pattern
// Always wrap errors with context
if err != nil {
return fmt.Errorf("loading config %s: %w", path, err)
}
// Define sentinel errors
var ErrNotFound = errors.New("not found")
// Check specific errors
if errors.Is(err, ErrNotFound) {
// Handle not found
}
Struct Patterns
For optional parameters, use functional options:
type Option func(*StatusLine)
func WithTheme(theme string) Option {
return func(s *StatusLine) {
s.theme = theme
}
}
func NewStatusLine(options ...Option) *StatusLine {
s := &StatusLine{theme: "default"} // defaults
for _, opt := range options {
opt(s)
}
return s
}
Memory Optimization
- Preallocate slices when size is known:
make([]T, 0, expectedSize) - Use
strings.Builderfor concatenation - Use
sync.Poolfor frequently allocated objects
LEVER Decision Framework
Before writing new code, apply the LEVER principles:
L - Leverage existing patterns (use what works)
E - Extend before creating (build on existing)
V - Verify through reactivity (self-validating systems)
E - Eliminate duplication (of knowledge, not just code)
R - Reduce complexity (simplest solution wins)
Quick decision guide:
- Leverage: Does the standard library solve this? Does an existing internal package?
- Extend: Can we extend existing code rather than create new?
- Verify: Will this be self-validating through reactive patterns?
- Eliminate: Am I duplicating business knowledge?
- Reduce: Is this the simplest solution?
Tip: The
search-firstskill provides a systematic workflow for the Leverage step.
Note: Duplicate code is acceptable if it represents different knowledge.
Detailed Standards
For complete Go idioms, see go-specific.md For interface design, see interfaces.md For documentation standards, see documentation.md For project coding guidelines, see CODING_GUIDELINES.md
Converted and distributed by TomeVault — claim your Tome and manage your conversions.