go-functional-options

Implement Go functional options pattern with unexported struct and With* constructors.

Updated Feb 28, 2026
One-click install
npx skills add https://github.com/phant-app/phant --skill go-functional-options-phant-app
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-functional-options
Source: https://github.com/phant-app/phant/tree/main/.agents/skills/go-functional-options
Command: npx skills add https://github.com/phant-app/phant --skill go-functional-options-phant-app

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This skill clarifies how to design Go APIs using the functional options pattern to cleanly handle optional parameters and extensibility, avoiding long parameter lists and tangled constructors.

Core Features & Use Cases

  • Flexible API configuration: Introduce an opaque options struct, an exported Option interface with an unexported apply method, and With* constructors to compose configuration.
  • Extensible constructors: Add new options without breaking existing calls, enabling scalable API evolution.
  • Safe defaults and validation: Provide sensible defaults and ensure options are validated where needed.

Quick Start

Open a new API instance by passing With* options to customize behavior.

Frequently Asked Questions about go-functional-options

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

FAQPage Schema
How do I handle optional parameters in Go constructors without breaking existing calls?

The functional options pattern handles optional parameters in Go constructors by passing With* functions to a base constructor, allowing you to add new configuration incrementally without altering existing call sites.

What is the functional options pattern in Go API design?

The functional options pattern in Go API design uses an unexported options struct and an exported Option interface with an unexported apply method, enabling flexible configuration composition through With* constructors while maintaining safe defaults.

When should I use functional options instead of a regular config struct in Go?

You should use functional options in Go when building public APIs with three or more parameters where extensibility matters, allowing you to add options over time without breaking existing callers or relying on long parameter lists.

How do I set default values and validate options using the functional options pattern?

To set default values using the functional options pattern, define an unexported options struct initialized with sensible defaults, then apply With* constructors in order within the constructor function to override defaults and validate the final configuration.

Can I add new configuration options to a Go API without causing breaking changes?

Yes, you can add new configuration options to a Go API without breaking changes by introducing additional With* constructors that implement the Option interface, keeping existing calls unchanged while extending the API's behavior.