golang-design-patterns

Applies idiomatic Go design patterns for constructors, error flow, resource management, and architecture.

Updated May 9, 2026
One-click install
npx skills add https://github.com/LuminaVault/LuminaVaultShared --skill golang-design-patterns-luminavault
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: golang-design-patterns
Source: https://github.com/LuminaVault/LuminaVaultShared/tree/main/.agents/skills/golang-design-patterns
Command: npx skills add https://github.com/LuminaVault/LuminaVaultShared --skill golang-design-patterns-luminavault

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Go developers often struggle to choose the right architectural pattern, misuse init() and global state, or write constructors that break as APIs evolve. This Skill provides opinionated, idiomatic Go design guidance so code stays simple, testable, and production-safe. ## Core Features & Use Cases - Constructor & Initialization Patterns: Functional options with error-returning validation, 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, ask which patterns fit — the Skill recommends functional options for the server constructor, streaming iterators for large data exports, and hexagonal architecture when multiple entry points (HTTP, gRPC, message consumer) are needed. ## Quick Start Ask the agent to design a Go HTTP server constructor with optional 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 pattern in Go?▼

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

Hexagonal architecture fits services with multiple entry points (HTTP, gRPC, message consumers) using primary and secondary ports. Clean architecture fits when you need strict layered dependency rules; both keep domain logic free of infrastructure imports.

Why should I avoid init() in Go for database setup?▼

init() runs implicitly before main and tests, cannot return errors, and creates hidden global state that makes testing unpredictable. Use explicit constructors like NewUserRepository(db) with dependency injection instead.

When should I panic vs 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; Must* constructors are a valid panic use case at init time.

How do I stream large datasets in Go without running out of memory?▼

Use iter.Seq2 iterators (Go 1.23+) or rows.Next() loops to yield one record at a time, writing each to the ResponseWriter instead of loading everything into a slice. This keeps memory constant and prevents OOM on million-row exports.

When is DDD overkill for a Go project?▼

DDD is overkill for simple CRUD apps, CLI tools, or projects under a few thousand lines. Reserve aggregates, value objects, and bounded contexts for complex domains with rich business rules, typically 5K+ lines or multiple contexts.