golang-patterns

Applies idiomatic Go patterns for error handling, concurrency, interfaces, and package design.

Updated Mar 26, 2026
One-click install
npx skills add https://github.com/erwinv2k-TKG/AgentesVSC --skill golang-patterns-erwinv2k-tkg
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-patterns
Source: https://github.com/erwinv2k-TKG/AgentesVSC/tree/main/packs/everything-claude-code/docs/zh-TW/skills/golang-patterns
Command: npx skills add https://github.com/erwinv2k-TKG/AgentesVSC --skill golang-patterns-erwinv2k-tkg

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing Go code that is merely functional but not idiomatic leads to maintenance pain, subtle concurrency bugs, and code review friction. This Skill provides a reference of idiomatic Go patterns so generated or reviewed code follows community conventions from the start. ## Core Features & Use Cases - Error Handling Patterns: Error wrapping with context, custom error types, sentinel errors, and proper use of errors.Is and errors.As. - Concurrency Patterns: Worker pools, context-based cancellation and timeouts, graceful shutdown, errgroup coordination, and goroutine leak prevention. - Design & Performance Guidance: Small interfaces defined at the consumer, functional options pattern, struct embedding, sync.Pool usage, and slice preallocation. - Use Case: When reviewing a Go service that spawns goroutines without cancellation handling, use this Skill to refactor it with context propagation, buffered channels, and errgroup coordination. ## Quick Start Review my Go HTTP server code and refactor it to follow idiomatic Go patterns for error handling and graceful shutdown.

Frequently Asked Questions about golang-patterns

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

FAQPage Schema
How do I handle errors idiomatically in Go?

Wrap errors with context using fmt.Errorf and the %w verb, define sentinel errors with errors.New for common cases, and create custom error types for domain-specific failures. Check errors with errors.Is and errors.As instead of string comparison, and never silently ignore errors with the blank identifier.

How do I prevent goroutine leaks in Go?

Use buffered channels or select statements with ctx.Done() so goroutines can exit when the context is cancelled. Coordinate goroutines with errgroup or sync.WaitGroup, and always ensure channel sends have a receiver or a cancellation path.

What is the functional options pattern in Go?

The functional options pattern passes configuration as variadic Option functions to a constructor, letting callers set only the fields they need while defaults apply to the rest. It avoids long parameter lists and keeps constructors extensible without breaking existing callers.

Should Go interfaces be defined by the provider or the consumer?

Interfaces should be defined at the point of use, in the consumer package, describing only what that consumer needs. This keeps interfaces small, avoids coupling packages to concrete implementations, and makes testing with mocks straightforward.

When should I use sync.Pool in Go?

Use sync.Pool for frequently allocated temporary objects like buffers in hot paths, such as request processing. Always reset the object before returning it to the pool, and avoid it for objects holding state that must not be shared.

What linters should I run on Go code?

Run gofmt and goimports for formatting, go vet for suspicious constructs, and golangci-lint with linters like errcheck, staticcheck, govet, ineffassign, and unused. Enable errcheck type-assertion checks and govet shadowing detection for stricter analysis.