cxuu avatar

Go Functional Options

Go constructor pattern for optional configuration via an unexported options struct and With* functions.

skills.sh installs
1.3K
GitHub stars
196
last updated
Aug 21, 2026
View on GitHub

Own this skill? Claim it

What it does

  • Covers the functional options pattern: an unexported options struct, an exported Option interface, and With* constructors
  • Includes a comparison table against config structs and a rationale for the interface approach over bare closures
  • Enforces an unexported apply method so only options defined in the package can be passed to the constructor
  • Designed for Go constructors with 3 or more optional parameters or public APIs expected to gain options over time

Install

$ npx -y skills add cxuu/golang-skills --skill go-functional-options -y

Installs into .agents/skills for every agent it finds here, and .claude/skills for Claude Code.

Files

Functional Options Pattern

Functional options is a pattern where you declare an opaque Option type that records information in an internal struct. The constructor accepts a variadic number of these options and applies them to configure the result.

Resource Routing

  • references/OPTIONS-VS-STRUCTS.md - Read when choosing between config structs and functional options, implementing the full interface-based option pattern, or evaluating hybrid constructor APIs.

When to Use

Use functional options when:

  • 3+ optional arguments on constructors or public APIs
  • Extensible APIs that may gain new options over time
  • Clean caller experience is important (no need to pass defaults)

The Pattern

Core Components

  1. Unexported options struct - holds all configuration
  2. Exported Option interface - with unexported apply method
  3. Option types - implement the interface
  4. With* constructors - create options

Option Interface

type Option interface {
    apply(*options)
}

The unexported apply method ensures only options from this package can be used.

Comparison: Functional Options vs Config Struct

Details

Repository
github.com/cxuu/golang-skills
Category
Go
First seen
May 16, 2026

More from cxuu/golang-skills

All 20

Similar to Go Functional Options

More Go