go-patterns

Write idiomatic Go patterns and audit code for anti-patterns and technical debt.

1|2|Updated Nov 25, 2017
One-click install
npx skills add https://github.com/asarchami/dotfiles --skill go-patterns-asarchami
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-patterns
Source: https://github.com/asarchami/dotfiles/tree/main/dot_config/opencode/skills/go/go-patterns
Command: npx skills add https://github.com/asarchami/dotfiles --skill go-patterns-asarchami

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go codebases drift away from idiomatic style over time, accumulating smells like goroutine leaks, discarded errors, unbounded resources, and bloated interfaces. This Skill provides the canonical Go idiom for constructors, resource lifecycle, graceful shutdown, and resilience, plus a severity-ranked anti-pattern checklist for auditing existing code. ## Core Features & Use Cases - Idiomatic pattern guidance: Covers functional options constructors, explicit lifecycle management, bounded resources, timeouts, retries with context checks, and panic-vs-error policy. - Anti-pattern audit checklist: A 40+ item scan table with severity ratings and detection hints covering abstraction, coupling, error handling, concurrency, testing, and security smells. - Architecture references: In-depth guides for clean architecture, hexagonal architecture, DDD, data handling, and resource management with full Go code examples. - Use Case: When reviewing a pull request, run the anti-pattern checklist against the diff to flag issues like wg.Add inside goroutines, missing http.MaxBytesReader, or math/rand used for tokens, each with a concrete fix. ## Quick Start Ask the assistant to audit your Go package using the go-patterns anti-pattern checklist and report each finding with severity, location, and the idiomatic replacement.

Frequently Asked Questions about go-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write idiomatic Go constructors with optional configuration?

Use functional options: define `type Option func(*Server)` with `With*` builder functions, and a `NewServer(addr string, opts ...Option)` constructor that applies defaults first. Return an error from options only when validation can fail, and reserve the builder pattern for complex multi-step validation.

How do I audit Go code for common anti-patterns?

Walk the anti-pattern checklist against the changed code, checking each category: abstraction, coupling, error handling, concurrency, testing, and security. Score each finding by severity, note the file and line, and record the idiomatic replacement pattern for each issue.

When should I use clean architecture vs hexagonal architecture in Go?

Use clean architecture for medium-to-large services needing strong separation between business logic and infrastructure via use cases and the dependency rule. Choose hexagonal architecture when the same domain logic must serve multiple entry points like HTTP, gRPC, and message consumers through ports and adapters.

Should I use init() for setup in Go packages?

No. `init()` runs implicitly, cannot return errors, and makes tests unpredictable since it executes before main and test functions. Use an explicit constructor like `New(dsn) (*Repo, error)` that returns errors and accepts injected dependencies.

Why is math/rand unsafe for generating tokens in Go?

math/rand is a deterministic pseudo-random generator, so tokens and keys derived from it are predictable to attackers. Use crypto/rand instead: read bytes with crypto/rand.Read into a byte slice and encode with hex or base64.

How do I implement graceful shutdown for a Go HTTP server?

Use signal.NotifyContext with SIGINT and SIGTERM, start the server in a goroutine, then block on context cancellation. Call srv.Shutdown with a timeout context to drain in-flight requests, then close remaining resources like database connections in order.