golang-patterns

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

Updated Mar 18, 2026
One-click install
npx skills add https://github.com/freedom909/real-estate-saas --skill golang-patterns-freedom909
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: golang-patterns
Source: https://github.com/freedom909/real-estate-saas/tree/main/.trae/skills/golang-patterns
Command: npx skills add https://github.com/freedom909/real-estate-saas --skill golang-patterns-freedom909

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing Go code that is merely functional is easy, but writing idiomatic Go that is maintainable, efficient, and consistent with community conventions requires deep knowledge of patterns that are easy to get wrong, such as goroutine leaks, improper error wrapping, and poorly designed interfaces. ## Core Features & Use Cases - Error Handling Guidance: Enforces error wrapping with context, sentinel errors, custom error types, and correct use of errors.Is and errors.As. - Concurrency Patterns: Provides worker pools, context-based cancellation, graceful shutdown, errgroup coordination, and goroutine leak prevention. - Design Conventions: Covers interface design, package organization, functional options, struct embedding, and performance optimizations like sync.Pool and slice preallocation. - Use Case: When reviewing a Go pull request, activate this Skill to check that errors are wrapped with context, goroutines handle cancellation properly, and interfaces are defined where they are consumed. ## Quick Start Review my Go service code and refactor it to follow 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 silently ignore returned errors.

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. Coordinate multiple goroutines with errgroup or sync.WaitGroup, and always ensure channel sends have a guaranteed receiver or cancellation path.

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.

Should Go functions accept interfaces or structs?▼

Functions should accept interfaces as parameters and return concrete struct types. Define interfaces in the consumer package where they are used, keeping them small and focused, often with a single method like io.Reader or io.Writer.

When should I use context.Context in Go functions?▼

Pass context as the first parameter of any function that performs I/O, network calls, or long-running work. Use context.WithTimeout for deadlines and never store context inside a struct; it should flow explicitly through the call chain.

Why avoid string concatenation in Go loops?▼

String concatenation with += creates a new allocation on every iteration, causing quadratic memory behavior. Use strings.Builder for a single allocation, or strings.Join when combining a slice of strings with a separator.