go-constructors

Demonstrate Go constructor patterns including functional options and builders.

2|Updated Feb 14, 2025
One-click install
npx skills add https://github.com/gofhir/validator --skill go-constructors
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-constructors
Source: https://github.com/gofhir/validator/tree/main/.claude/skills/go-constructors
Command: npx skills add https://github.com/gofhir/validator --skill go-constructors

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps developers write cleaner, more flexible, and maintainable Go code by demonstrating various patterns for creating objects and configuring APIs.

Core Features & Use Cases

  • Constructor Patterns: Covers simple New*, panic-on-error MustNew*, versioned constructors, option structs, functional options (WithX), and builder patterns.
  • API Design: Illustrates how to create configurable APIs and composite services.
  • Use Case: When designing a new Go library, use this Skill to understand the best way to provide constructors that allow users to easily configure the library's behavior without breaking existing code.

Quick Start

Show me examples of using functional options to configure a Go struct.

Frequently Asked Questions about go-constructors

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

FAQPage Schema
What is the best way to configure a Go struct using functional options?

Go constructor patterns range from simple `New*` functions for basic initialization to `MustNew*` for panic-on-error scenarios, and builder patterns for complex, multi-step object assembly.

When should I use functional options versus an option struct in Go?

Use functional options (`WithX`) for extensible API design where defaults are common, whereas option structs are better for configurations requiring multiple mandatory fields or simpler struct initialization.

How do I design an extensible Go library API without breaking existing code?

Design extensible Go library APIs using functional options and versioned constructors, allowing you to add new configuration parameters without changing existing function signatures or breaking callers.

Can I use a panic-on-error constructor for Go object initialization?

Yes, `MustNew*` constructors panic on error during object initialization, making them suitable for Go scenarios where configuration failures are irrecoverable and should halt program execution immediately.

What are the limitations of using builder patterns for Go object construction?

Builder patterns in Go add structural complexity and require more boilerplate code than simple constructors, making them less ideal for lightweight object initialization or straightforward API configuration.