Go Functional Options Pattern

•
57
•

Implements idiomatic, extensible, and type-safe functional options patterns for Go constructors and factory functions.

This skill provides domain-specific guidance for implementing the Functional Options pattern in Go, distilled from the Uber Style Guide and Google's production best practices. It helps developers move beyond cluttered constructors and fragile config structs by implementing a robust pattern involving unexported options structs and exported Option interfaces. This approach ensures your Go APIs remain backward-compatible even as new configuration parameters are added, providing a clean caller experience while maintaining high testability and strict type safety.

主要功能

01Standardizes the creation of variadic With* option functions
02Implements the interface-based approach for superior testability and mockability
03Enforces clear separation between required parameters and optional settings
04Ensures API backward compatibility for future-proof library design
05Automates unexported options structs with sensible default values
0657 GitHub stars

使用场景

01Building production-grade clients or database connectors following Uber's Go style guide
02Refactoring complex constructors with 3+ arguments into clean, readable functional options
03Designing public Go libraries that require extensible configuration without breaking changes