golang-patterns

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

Updated Mar 25, 2026
One-click install
npx skills add https://github.com/Femad-6/my-skills --skill golang-patterns-femad-6
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-patterns
Source: https://github.com/Femad-6/my-skills/tree/main/.github/skills/golang-patterns
Command: npx skills add https://github.com/Femad-6/my-skills --skill golang-patterns-femad-6

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing Go code that follows community conventions is hard when you are unsure about error wrapping, goroutine lifecycle management, interface placement, or project layout. This Skill supplies concrete, idiomatic Go patterns so code reviews, refactors, and new development follow accepted best practices. ## Core Features & Use Cases - Error Handling Patterns: Error wrapping with fmt.Errorf and %w, 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 (accept interfaces, return structs), functional options pattern, struct embedding, standard project layout, and performance tips like slice preallocation and sync.Pool. - Use Case: When reviewing a Go pull request that spawns goroutines without cancellation handling, use this Skill to rewrite the code with context propagation and errgroup so leaks and unhandled errors are eliminated. ## Quick Start Ask the assistant to review your Go file using golang-patterns and refactor it to follow idiomatic error handling and concurrency conventions.

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 them with errors.Is and errors.As instead of string comparison, and never silently ignore returned errors.

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 multiple goroutines with errgroup or sync.WaitGroup, and always ensure every spawned goroutine has a defined termination path.

What is the standard Go project layout?

A common layout places the entry point in cmd/myapp, private code in internal/ (handler, service, repository, config), public libraries in pkg/, API definitions in api/, and test fixtures in testdata/. Keep package names short, lowercase, and free of underscores.

Should Go functions accept interfaces or structs?

Functions should accept interfaces and return concrete structs. Define small, focused interfaces in the consumer package rather than the provider package, and use type assertions for optional behavior like Flusher.

When should I use the functional options pattern in Go?

Use functional options when a constructor has many optional parameters with sensible defaults, such as server timeouts or loggers. It keeps the API extensible without breaking existing callers when new options are added.

Which linters should I run on Go code?

Run gofmt and goimports for formatting, go vet for suspicious constructs, and golangci-lint with errcheck, staticcheck, gosimple, ineffassign, and unused enabled. Also run go test with -race and -cover for concurrency and coverage checks.