golang-patterns

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

2|Updated Mar 29, 2015
One-click install
npx skills add https://github.com/ovisan/dotfiles --skill golang-patterns-ovisan
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-patterns
Source: https://github.com/ovisan/dotfiles/tree/main/.agents/skills/golang-patterns
Command: npx skills add https://github.com/ovisan/dotfiles --skill golang-patterns-ovisan

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, functional options pattern, package organization, 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, handles goroutine cancellation properly, and follows standard project layout conventions. ## 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 create custom error types for domain-specific failures. Check errors with errors.Is and errors.As rather than string comparison, and never 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. Unbuffered channel sends block forever if no receiver exists, which is the most common leak source. The errgroup package helps coordinate goroutines and propagate cancellation.

What is the standard Go project layout?

Place entry points in cmd/, private application 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 or redundant suffixes.

Should Go functions accept interfaces or structs?

Functions should accept interfaces and return concrete structs. Define interfaces in the consumer package where they are used, not the provider package, and keep interfaces small with one or two methods that can be composed as needed.

When should I use the functional options pattern in Go?

Use functional options when a constructor has multiple optional parameters with sensible defaults, such as server timeouts or loggers. It keeps the constructor signature stable while allowing callers to override only the settings they need.