go-functional-options

Implement Go functional options with With* constructors and an Option interface.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Go constructors often require many optional parameters, leading to brittle and hard-to-read call sites. Functional options pattern solves this by using an opaque options struct, an exported Option interface, and With* constructors to apply configuration while keeping sensible defaults.

Core Features & Use Cases

  • Extensible API design with With* options
  • Centralized defaults management
  • Clear, readable caller experience

Quick Start

Create a minimal Go example that defines an unexported options struct, an exported Option interface with apply, With* option constructors, and a constructor that applies options to defaults.

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 multiple optional parameters in a Go constructor?

The functional options pattern solves Go API configuration complexity by using an unexported options struct and exported With* constructors. Callers only configure what differs from the defaults, keeping call sites readable and extensible.

What is the functional options pattern in Golang?

The functional options pattern in Golang uses an exported Option interface with an apply method and With* constructors to configure an unexported options struct. This allows callers to override specific defaults without passing multiple parameters to a constructor.

How do I create extensible Go APIs with default configuration?

You create extensible Go APIs by defining an unexported options struct for defaults, an exported Option interface, and With* constructors. A base constructor then applies these functional options to the defaults, allowing callers to configure only what they need.

How do functional options improve Golang library design?

Functional options improve Golang library design by centralizing default management and avoiding brittle constructors with many parameters. They provide a clear, readable caller experience where developers only specify the configurations that differ from the defaults.

Does the functional options pattern require external dependencies in Go?

No, the functional options pattern does not require external dependencies in Go. It is implemented purely with standard language features: an unexported options struct, an exported Option interface with an apply method, and With* option constructors.

When should I not use functional options for API construction?

You should avoid functional options for API construction when a Go struct requires no optional parameters or has strictly mandatory fields. The pattern is designed specifically for scenarios with multiple optional configurations where defaults should be preserved.