golang-design-patterns

Guide Go service architecture with idiomatic design patterns and resource lifecycle practices.

1|Updated Mar 21, 2026
One-click install
npx skills add https://github.com/dashkan/pivox --skill golang-design-patterns-dashkan
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-design-patterns
Source: https://github.com/dashkan/pivox/tree/main/.agents/skills/golang-design-patterns
Command: npx skills add https://github.com/dashkan/pivox --skill golang-design-patterns-dashkan

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

It helps you structure production Go code consistently by recommending idiomatic design patterns that reduce hidden complexity and runtime surprises.

Core Features & Use Cases

  • Functional options over builders: Create constructor APIs that scale as configuration grows, with options that can validate and fail fast.
  • Safer initialization and lifecycle: Avoid init() for hidden side effects, ensure resource cleanup with defer Close() right after opening, and prefer graceful shutdown/resilience practices.
  • Robust error and resource handling: Handle error cases early, use timeouts for external calls, retry responsibly (checking context cancellation), and limit unbounded resources to prevent crashes.
  • Testable architecture and dependency injection: Design for testability by accepting interfaces and injecting dependencies rather than relying on implicit globals.

Quick Start

Use this skill when you are implementing a Go service component and want guidance on which idiomatic pattern fits—especially for constructor APIs (functional options), validation/error flow, and graceful shutdown/resource lifecycle.

Frequently Asked Questions about golang-design-patterns

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

FAQPage Schema
What is the best way to structure a Go constructor API as configuration grows?

Use functional options for your Go constructor API to scale configuration cleanly. This pattern allows options to validate and fail fast during initialization, avoiding the complexity of traditional builders.

How do I handle resource lifecycle and graceful shutdown in Go production services?

Handle Go resource lifecycle by calling defer Close() immediately after opening resources. Implement graceful shutdown and resilience practices with context-aware cancellation to prevent unbounded resource usage and crashes.

Why should I avoid using init() for Go package initialization?

Avoid init() because it introduces hidden side effects and implicit dependencies. Safer Go initialization prefers explicit constructor APIs and dependency injection, ensuring testability and predictable startup behavior.

How do I implement context-aware retry behavior for external calls in Go?

Implement context-aware retry behavior in Go by checking context cancellation before retrying external calls. Enforce timeouts on external calls to handle errors early, retry responsibly, and prevent unbounded resource usage.

How do I design Go services for testability using dependency injection?

Design Go services for testability by accepting interfaces and injecting dependencies rather than relying on implicit globals. This architecture enables robust error-first flow and allows components to be easily mocked.