golang-patterns

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

5|Updated Jul 8, 2019
One-click install
npx skills add https://github.com/rinchsan/dotfiles --skill golang-patterns-rinchsan
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-patterns
Source: https://github.com/rinchsan/dotfiles/tree/main/.claude/skills/golang-patterns
Command: npx skills add https://github.com/rinchsan/dotfiles --skill golang-patterns-rinchsan

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing Go code that follows community conventions is hard without a reference, leading to inconsistent error handling, goroutine leaks, and poorly designed packages that are difficult to maintain and review. ## Core Features & Use Cases - Error Handling Patterns: Error wrapping with context, custom error types, sentinel errors, and errors.Is/errors.As checking. - Concurrency Patterns: Worker pools, context-based cancellation and timeouts, graceful shutdown, errgroup coordination, and goroutine leak prevention. - Design Guidance: Interface design, package organization, functional options pattern, struct embedding, and performance techniques like sync.Pool and slice preallocation. - Use Case: When reviewing a Go pull request, activate this Skill to check whether the code wraps errors with context, avoids goroutine leaks, and follows the accept-interfaces-return-structs convention. ## Quick Start Review my Go service code and suggest improvements based on idiomatic Go patterns for error handling and concurrency.

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 check them with errors.Is and errors.As. Never ignore errors with the blank identifier unless explicitly documented.

How to prevent goroutine leaks in Go?

Use buffered channels or select statements with ctx.Done() so goroutines can exit when the context is cancelled. An unbuffered channel send with no receiver blocks forever, leaking the goroutine.

What is the functional options pattern in Go?

The functional options pattern configures structs by passing variadic Option functions to a constructor, each setting one field. It provides clean defaults and extensible APIs without long parameter lists or config structs.

Should Go functions accept interfaces or structs?

Functions should accept interfaces as parameters and return concrete struct types. Define small interfaces in the consumer package where they are used, not in the provider package.

When should I use errgroup instead of sync.WaitGroup?

Use errgroup when you need coordinated goroutines that propagate the first error and cancel a shared context. sync.WaitGroup only waits for completion and does not handle error propagation or cancellation.

What linters should I run on Go code?

Run go vet, staticcheck, and golangci-lint with linters like errcheck, gosimple, ineffassign, and unused. Always format code with gofmt or goimports before committing.