Go code quality standards, idioms, and YAGNI/KISS development principles.
go fmt after changes, use goimportsUserService, GetUser, ErrNotFoundHTTP, ID, URLRequirements:
%w for context: fmt.Errorf("operation failed: %w", err)var ErrNotFound = errors.New("not found")errors.Is() / errors.As()func (s *Service) GetUser(ctx context.Context, id int) (*User, error) {
user, err := s.repo.FindByID(ctx, id)
if err != nil {
return nil, fmt.Errorf("failed to get user: %w", err)
}
return user, nil
}
ctx context.Contextpackage user with Service, Repository, Handlerpackage services with all servicestests := []struct {
name string
a, b int
want int
}{
{"positive", 1, 2, 3},
{"negative", -1, -2, -3},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Add(tt.a, tt.b)
if got != tt.want {
t.Errorf("Add(%d, %d) = %d; want %d", tt.a, tt.b, got, tt.want)
}
})
}
| Pattern | Example | Fix |
|---------|---------|-----|
| Global State | var defaultX = ... | Immutable init() only |
| Naked Goroutines | go func(){}() | Context cancellation |
| Ignored Errors | result, _ := op() | Handle or document |
| Defer in Loops | defer in for | Manual cleanup |
| String Concat | s += s2 in loops | strings.Builder |
| Large Interfaces | >3 methods | Split or rethink |
| Over-engineering | Factory for simple constructor | Direct New() |
sync.Pool: Use for hot paths; always Reset() before Put().
Zero-alloc: Pre-allocate slices (make([]T, 0, cap)), use strings.Builder, prefer []byte.
Profile: go test -cpuprofile=cpu.prof -bench=. → go tool pprof → optimize → repeat.
type Server struct {
host string
port int
timeout time.Duration
}
type Option func(*Server)
func WithPort(port int) Option {
return func(s *Server) { s.port = port }
}
func NewServer(opts ...Option) *Server {
s := &Server{
host: "localhost",
port: 8080,
timeout: 30 * time.Second,
}
for _, opt := range opts {
opt(s)
}
return s
}
// Usage
srv := NewServer(WithPort(3000))
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer