Skip to content

Instantly share code, notes, and snippets.

@patrickhuber
Last active March 28, 2025 02:10
Show Gist options
  • Save patrickhuber/b7d570b0741f31982fb02f1698cd305e to your computer and use it in GitHub Desktop.
Save patrickhuber/b7d570b0741f31982fb02f1698cd305e to your computer and use it in GitHub Desktop.
Interface Options with Reuse
package main
type option map[string]any
const transformerKey string = "transformer"
func (option) transformerOption() {}
type TransformerOption interface {
transformerOption()
}
type Transformer interface{
Transform(any) (any, error)
}
func WithTransformer(transformer Transformer) option {
return option{
transformerKey: transformer,
}
}
type ProviderOption interface {
providerOption()
}
func (option) providerOption() {}
func WithProvider() ProviderOption {
return option{}
}
func NewThing(options ...ProviderOption) {
}
func UseThing() {
NewThing(WithTransformer(nil), WithProvider())
}
type OtherOption interface {
otherOption()
}
func (option) otherOption() {}
func WithOther() OtherOption {
return option{}
}
func NewOther(options ...OtherOption) {
}
func UseOther() {
NewOther(WithTransformer(nil), WithProvider(), WithOther())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment