golang-design-patterns

Guides idiomatic Go design decisions covering constructors, error flow, resource management, and architecture patterns.

1|Updated May 25, 2020
One-click install
npx skills add https://github.com/titaneric/dotfiles --skill golang-design-patterns-titaneric
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-design-patterns
Source: https://github.com/titaneric/dotfiles/tree/main/dot_agents/skills/golang-design-patterns
Command: npx skills add https://github.com/titaneric/dotfiles --skill golang-design-patterns-titaneric

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go developers often face recurring design decisions — constructor APIs, enum definitions, resource cleanup, graceful shutdown, and architecture selection — where the wrong default leads to brittle, untestable, or over-engineered code. This Skill encodes idiomatic Go design patterns so an AI coding agent applies the right pattern for the problem instead of generic or over-abstracted solutions. ## Core Features & Use Cases - Constructor & Initialization Patterns: Functional options over builders, avoiding init(), enums starting at 1, compile-time interface checks, and package-level regexp compilation. - Resource & Resilience Patterns: Immediate defer Close(), runtime.AddCleanup over SetFinalizer, bounded channel-based pools, timeouts on external calls, context-aware retries, and graceful shutdown via signal.NotifyContext. - Architecture Guidance: Right-sized architecture selection (flat, layered, clean, hexagonal, DDD) with detailed reference guides, aggregate roots, value objects, bounded contexts, and anti-corruption layers. - Use Case: When designing a new Go HTTP service, the agent asks about architecture preference, applies functional options for server configuration, sets up graceful shutdown with signal handling, and streams large database exports instead of loading everything into memory. ## Quick Start Ask the agent to design a Go HTTP server constructor with configurable timeouts and connection limits using idiomatic patterns.

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 constructors?

Define an Option type as func(*Server), write With* functions returning Options, and accept variadic ...Option in the constructor. Set defaults first, then apply options. Options should return an error when validation can fail.

Should I use clean architecture or hexagonal architecture in Go?

Use hexagonal architecture when the same business logic needs multiple entry points like HTTP, gRPC, and message consumers, since ports and adapters isolate each driver. Clean architecture fits when you need strict layered dependency rules. Skip both for small CLIs.

Why should Go enums start at 1 instead of 0?

Go's zero value would silently pass as the first enum member if it held a meaningful value. Start enums at 1 or place an Unknown sentinel at 0 so uninitialized values are detectable as invalid.

When should I panic versus return an error in Go?

Return errors for expected failures callers can handle, such as invalid input or network issues. Panic only for bugs like violated invariants or impossible nil values, and in Must* constructors used at init time.

Does Go 1.24 change how finalizers should be written?

Yes, Go 1.24 adds runtime.AddCleanup, which is preferred over runtime.SetFinalizer. AddCleanup supports multiple cleanups per object, avoids object resurrection by passing a copy of the value, and works with cyclic references.

How do I avoid loading millions of database rows into memory in Go?

Stream results instead of collecting them into a slice. Use iter.Seq2 iterators (Go 1.23+) or a rows.Next() loop that encodes each record directly to the HTTP response, keeping memory usage constant and preventing OOM.