Functional Options Pattern utility for Go.
go get github.com/tingtt/optionsimport "github.com/tingtt/options"- Simple one
type Option struct {} func NewService(_options ...options.Applier[Option]) (Service, error) { option := options.Create(_options...) // Create option from functional options. service := new(service) // effect option to service ... return service, nil }
- Create option with default value
type Option struct {} func DefaultOption() Option { return Option{ // write default fields... } } func NewService(_options ...options.Applier[Option]) (Service, error) { option := options.CreateWithDefault(DefaultOption(), _options...) // Create option from functional options. service := new(service) // effect option to service ... return service, nil }