golang-design-patterns

Guide Go API design with functional options, validation, and runtime cleanup.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Designing Go APIs and applications without clear patterns leads to hidden bugs, unmaintainable code, and resource leaks. This skill helps developers choose the right idiomatic patterns and avoid common pitfalls.

Core Features & Use Cases

  • Functional Options: Enables scalable constructors with validation.
  • Init Avoidance: Promotes explicit initialization for testability.
  • Enum Practices: Starts enums at 1 with an unknown sentinel.
  • Runtime Cleanup: Recommends runtime.AddCleanup over SetFinalizer.
  • Resource Pools: Shows bounded channel pools and graceful shutdown.
  • Architecture Guidance: Advises right‑sizing architecture and choosing hexagonal versus clean approaches.

Quick Start

Ask the golang-design-patterns skill to suggest the appropriate constructor pattern for a new HTTP server with optional timeouts and logger.

Frequently Asked Questions about golang-design-patterns

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

FAQPage Schema
How do I implement functional options in Go for scalable constructors?

Functional options in Go enable scalable constructors with built-in validation by passing configuration functions to a struct. This pattern lets you set optional parameters like HTTP server timeouts while keeping defaults maintainable and explicitly initialized.

What's the best way to handle resource cleanup in Go instead of SetFinalizer?

Using runtime.AddCleanup is recommended over SetFinalizer for resource management in Go. It provides a more predictable mechanism for associating cleanup functions with objects, preventing resource leaks and hidden bugs without relying on garbage collector finalization timing.

Why should I avoid init functions in Go applications?

Avoiding init functions in Go promotes explicit initialization for better testability. Hidden side effects during package initialization lead to unmaintainable code and obscure bugs, whereas explicit setup allows developers to control state and dependencies clearly during runtime.

How do I design bounded resource pools with graceful shutdown in Golang?

Bounded channel pools in Golang manage resource limits by allocating a fixed number of resources through a buffered channel. This pattern ensures graceful shutdown by draining the channel and releasing resources safely, preventing leaks during application termination.

How do I structure Golang enums to avoid zero-value bugs?

Golang enums should start at 1 and include an unknown sentinel value to avoid zero-value bugs. Because Go initializes unassigned integers to zero, starting enums at 1 ensures the default zero value represents an explicitly unassigned state rather than a valid enum.

When do I need hexagonal architecture versus clean architecture in Go projects?

Choosing between hexagonal and clean architecture in Go depends on right-sizing your application's complexity. Hexagonal architecture isolates business logic from infrastructure using ports and adapters, while clean architecture layers dependencies, both preventing unmaintainable code in growing projects.