cxuu avatar

Go Functional Options

This teaches Claude the functional options pattern for Go constructors, the approach where you pass variadic Option types instead of sprawling parameter lists. It's well suited for any New* function with three or more optional parameters, or APIs that might grow over time. The skill covers the full implementation (unexported options struct, Option interface with unexported apply method, With* constructors) and includes a comparison table against config structs so you know when to use which. It's opinionated about preferring interfaces over closures for testability, and the examples are lifted from Uber's style guide. Good reference material if you're tired of constructors that take eight booleans.

skills.sh installs
1.3K
GitHub stars
193
last updated
Aug 21, 2026

Install

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

Installs into .claude/skills of the current project.

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

Sponsors

Details

Repository
github.com/cxuu/golang-skills
Categories
Go, Design & UI/UX
First seen
May 16, 2026

More from cxuu/golang-skills

All 20

Recommended

More Go
1cxuu avatargo-control-flowcxuu/golang-skillsUse when writing conditionals, loops, or switch statements in Go — including if with initialization, early returns, for loop forms, range, switch, type switches1.3Kskills.sh installs
2cxuu avatargo-loggingcxuu/golang-skillsUse when choosing a logging approach, configuring slog, writing structured log statements, or deciding log levels in Go. Also use when setting up production log1.1Kskills.sh installs
3cxuu avatargo-functionscxuu/golang-skillsUse when organizing functions within a Go file, formatting function signatures, designing return values, or following Printf-style naming conventions. Also use 1.1Kskills.sh installs
4cxuu avatargo-genericscxuu/golang-skillsUse when deciding whether to use Go generics, writing generic functions or types, choosing constraints, or picking between type aliases and type definitions. Al1.1Kskills.sh installs
5cxuu avatargo-declarationscxuu/golang-skillsUse when declaring or initializing Go variables, constants, structs, or maps — including var vs :=, reducing scope with if-init, formatting composite literals, 1.1Kskills.sh installs
6samber avatargolang-testingsamber/cc-skills-golangProduction-ready Golang tests — table-driven tests, testify suites and mocks, parallel tests, fuzzing, fixtures, goroutine leak detection with goleak, snapshot 45.4Kskills.sh installs