go-functional-options

Implement functional options pattern in Go APIs with With* constructors.

Updated Apr 16, 2026
One-click install
npx skills add https://github.com/HadiCherkaoui/opencode-config --skill go-functional-options-hadicherkaoui
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-functional-options
Source: https://github.com/HadiCherkaoui/opencode-config/tree/main/skills/golang/go-functional-options
Command: npx skills add https://github.com/HadiCherkaoui/opencode-config --skill go-functional-options-hadicherkaoui

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Eliminate long, brittle constructor parameter lists in Go APIs.

Core Features & Use Cases

  • Unexported internal options struct that holds configuration
  • Exported Option interface with an unexported apply method
  • With* constructors enabling fluent API configuration
  • Clear defaults and deterministic option application

Quick Start

Import the package and call Open with any With options to customize 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 optional parameters in Go constructors without long parameter lists?

To handle optional parameters in Go constructors, use the functional options pattern. It replaces brittle parameter lists by applying With* functions to an internal options struct, enabling flexible and evolving API configurations.

What is the functional options pattern in Go API design?

The functional options pattern in Go API design is a technique using an exported Option interface with an apply method. It modifies an unexported internal options struct through With* constructors to establish clear defaults and deterministic configuration.

When should I use functional options for Go APIs?

You should use functional options for Go APIs when a constructor or public function has 3 or more optional parameters. It is specifically applicable for evolving configurations where long parameter lists would otherwise become brittle and hard to maintain.

How do I set default configurations and apply options in a Go constructor?

To set default configurations in a Go constructor, establish clear defaults first, then apply exported Option values. Each option's apply method sequentially modifies the unexported internal options struct to compose the final configuration.

Does the functional options pattern work with existing Go structs?

Yes, the functional options pattern works by maintaining an unexported internal options struct that holds configuration. It wraps this internal state with an exported Option interface, keeping the underlying struct hidden while allowing fluent API configuration.