golang-idiomatic-go
Scannednpx machina-cli add skill saifoelloh/golang-best-practices-skill/idiomatic-go --openclawGolang Idiomatic Patterns
Expert-level review for idiomatic Go patterns. Ensures code follows Go conventions, proper interface design, and appropriate pointer usage.
When to Apply
Use this skill when:
- Ensuring code follows Go idioms
- Reviewing interface design patterns
- Checking pointer vs value usage
- Verifying "Accept interfaces, return structs" pattern
- Auditing usecase complexity
- Ensuring small, focused interfaces
Rule Categories by Priority
| Priority | Count | Focus |
|---|---|---|
| High | 1 | Method receivers |
| Medium | 5 | Go idioms and patterns |
Rules Covered (6 total)
High-Impact Patterns (1)
high-pointer-receiver- Use pointer receivers for mutations
Medium Improvements (5)
medium-interface-pollution- Keep interfaces small (<5 methods)medium-accept-interface-return-struct- API flexibility patternmedium-pointer-overuse- Don't overuse pointers for small typesmedium-usecase-complexity- Move business logic to domain entitiesmedium-interface-in-implementation- Define interfaces where used
Common Idiomatic Patterns
Accept Interfaces, Return Structs
// ✅ GOOD: Accept interface, return struct
func Process(r io.Reader) (*Result, error) {
// ...
return &Result{}, nil
}
Small Interfaces
// ✅ GOOD: Small, focused interface
type Reader interface {
Read(p []byte) (n int, err error)
}
Pointer Receivers for Mutations
// ✅ GOOD: Pointer receiver modifies state
func (u *User) UpdateEmail(email string) {
u.Email = email
}
Trigger Phrases
This skill activates when you say:
- "Is this idiomatic Go?"
- "Review Go style"
- "Check interface design"
- "Verify pointer usage"
- "Review Go conventions"
- "Check for Go anti-patterns"
How to Use
For Idiomatic Review
- Check method receivers (pointer vs value)
- Verify interface sizes and locations
- Review pointer usage appropriateness
- Check "accept interfaces, return structs" pattern
Output Format
## Idiomatic Go Improvements: X
### [Rule Name] (Line Y)
**Pattern**: Interface Design / Pointer Usage / Method Receiver
**Issue**: Violates Go idiom
**Fix**: Suggested correction
**Example**:
```go
// Idiomatic Go code
Related Skills
- golang-clean-architecture - For interface segregation
- golang-design-patterns - For usecase complexity
Philosophy
Based on "Learning Go" and Effective Go:
- Simplicity over cleverness - Boring code is good code
- Interfaces are satisfied implicitly - Define where used
- Composition over inheritance - Embed types, don't extend
- Clear is better than clever - Readability wins
Notes
- Focus on Go-specific patterns
- Not general programming practices
- Emphasizes Go conventions and idioms
- Based on Effective Go and community standards
Source
git clone https://github.com/saifoelloh/golang-best-practices-skill/blob/main/idiomatic-go/SKILL.mdView on GitHub Overview
This skill audits Go code for idiomatic patterns, focusing on interface design, pointer usage, and Go conventions. It helps ensure code follows Go community best practices and remains maintainable.
How This Skill Works
The skill analyzes code against a Go-specific checklist: appropriate method receivers (pointer vs value), interface sizes, pointer usage, and the accept-interfaces-return-struct pattern. It flags deviations from idioms and provides concrete, language-idiomatic fixes based on Effective Go guidance.
When to Use It
- Ensuring code follows Go idioms
- Reviewing interface design patterns
- Checking pointer vs value usage
- Verifying the accept interfaces, return structs pattern
- Auditing usecase complexity
Quick Start
- Step 1: Scan code for large interfaces and pointer-heavy mutation points
- Step 2: Check receivers (pointer vs value) and verify mutating methods use pointer receivers
- Step 3: Refactor to Accept interfaces, Return structs, and keep interfaces small where used
Best Practices
- Accept Interfaces, Return Structs: design APIs that consume interfaces and return concrete results
- Small Interfaces: keep interfaces focused and minimal (fewer than 5 methods)
- Pointer Receivers for Mutations: use pointer receivers when methods mutate state
- Don't overuse pointers for small types: prefer values when mutability isn’t needed
- Move business logic to domain entities: keep usecase logic out of interface layers
Example Use Cases
- Accept Interfaces, Return Structs: func Process(r io.Reader) (*Result, error) { ... return &Result{}, nil }
- Small Interfaces: type Reader interface { Read(p []byte) (n int, err error) }
- Pointer Receivers for Mutations: func (u *User) UpdateEmail(email string) { u.Email = email }
- Interface Segregation in Practice: define interfaces where used rather than a fat, global interface
- API Design: return concrete structs from functions instead of exposing broad interfaces