mirror of
https://github.com/sstent/go-garth.git
synced 2026-02-14 11:22:28 +00:00
2.5 KiB
2.5 KiB
Go Coding Style Guide
Code Organization & Structure
Logical Segmentation
- One concept per file (e.g., handlers, models, services).
- Keep functions under 20-30 lines when possible.
- Use meaningful package names that reflect functionality.
- Group related types and functions together.
Interface Design
- Prefer small, focused interfaces (1-3 methods).
- Define interfaces where they're used, not where they're implemented.
- Use composition over inheritance.
Conciseness Rules
Variable & Function Naming
- Use short names in small scopes (
i,err,ctx). - Longer names for broader scopes (
userRepository,configManager). - Omit obvious type information (
users []UsernotuserList []User).
Error Handling
// Prefer early returns
if err != nil {
return err
}
// over nested if-else blocks
Type Declarations
- Use type inference:
users := []User{}notvar users []User = []User{}. - Combine related variable declarations:
var (name string; age int).
Go Idioms for Conciseness
Zero Values
- Leverage Go's zero values instead of explicit initialization.
- Use
var buf bytes.Bufferinstead ofbuf := bytes.Buffer{}.
Struct Initialization
// Prefer struct literals with field names
user := User{Name: name, Email: email}
// over multiple assignments
Method Receivers
- Use pointer receivers for modification or large structs.
- Use value receivers for small, immutable data.
Channel Operations
- Use
selectwithdefaultfor non-blocking operations. - Prefer
rangeover explicit channel reads.
Context Reduction Strategies
Function Signatures
- Group related parameters into structs.
- Use functional options pattern for complex configurations.
- Return early and often to reduce nesting.
Constants and Enums
const (
StatusPending = iota
StatusApproved
StatusRejected
)
Embed Common Patterns
- Use
sync.Oncefor lazy initialization. - Embed
sync.Mutexin structs needing synchronization. - Use
context.Contextas first parameter in functions.
File Organization Rules
Package Structure
/cmd - main applications
/internal - private code
/pkg - public libraries
/api - API definitions
Import Grouping
- Standard library
- Third-party packages
- Local packages (Separate groups with blank lines)
Testing
- Place tests in same package with
_test.gosuffix. - Use table-driven tests for multiple scenarios.
- Keep test functions focused and named clearly.