go-functional-options

Design Go constructors using the functional options pattern.

8|Updated Jun 5, 2026
One-click install
npx skills add https://github.com/muratmirgun/gophers --skill go-functional-options-muratmirgun
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-functional-options
Source: https://github.com/muratmirgun/gophers/tree/main/skills/go-functional-options
Command: npx skills add https://github.com/muratmirgun/gophers --skill go-functional-options-muratmirgun

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps you design Go constructors and factories that can accept many optional settings without turning the API into a brittle pile of positional arguments. It guides you toward a stable pattern that preserves backward compatibility as your package evolves.

Core Features & Use Cases

  • Canonical functional options pattern: Use an unexported options bag, an Option interface with an unexported apply method, and With* constructors for each setting.
  • API design guidance: Decide when to use functional options versus a config struct, especially for constructors with 3+ optional parameters or future growth.
  • Production-oriented details: Set defaults before applying options, keep required parameters positional, and prefer interface-based options over closures for testability and introspection.
  • Use case: You are reviewing a New* function with many toggles for cache, logging, timeouts, and metrics, and need to refactor it into a cleaner, extensible API.

Quick Start

Ask the AI to review your Go constructor and rewrite it using functional options with sensible defaults, unexported option state, and With* helpers.

Frequently Asked Questions about go-functional-options

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

FAQPage Schema
How do I use the functional options pattern in Go to replace positional parameters?

The functional options pattern in Go replaces brittle positional parameters by using an unexported options struct, an Option interface with an unexported apply method, and With* constructors for each setting. This approach keeps required parameters positional while allowing optional settings to evolve safely.

When should I use functional options versus a config struct for Go API design?

Use functional options for Go API design when a constructor has three or more optional parameters or anticipates future growth. A config struct is simpler for fixed configurations, but functional options provide better extensibility and backward compatibility as your package interface evolves over time.

How do I maintain backward compatibility in Go constructors when adding new optional settings?

Maintain backward compatibility in Go constructors by setting default values in an unexported options struct before applying any functional options. This ensures existing calls remain valid when you introduce new With* helpers for optional settings like cache, logging, or metrics.

Why prefer interface-based functional options over closures for Go constructors?

Prefer interface-based functional options over closures in Go for enhanced testability and introspection. While closures are common, using an Option interface with an unexported apply method allows you to validate and inspect the applied settings more effectively during testing.

How do I refactor a Go constructor with many toggles into a flexible API?

Refactor a Go constructor with many toggles by extracting optional settings into an unexported options struct. Apply the functional options pattern by creating With* helpers for each toggle, keeping only required parameters positional, and ensuring defaults are set before options are applied.

What are the limitations of the functional options pattern in Go?

The functional options pattern in Go is less suited for constructors with only one or two static parameters where a config struct would be simpler. It also requires strict adherence to setting defaults before applying options to avoid nil pointer issues or unexpected behavior when optional settings are omitted.