Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Go Visitor Pattern to pass and apply options
type Logger struct {
Prop1 string
Prop2 string
}
type Option func(l *Logger) error
func Prop1Option() Option {
return func(l *Logger) error {
l.Prop1 = "hello"
return nil
}
}
func Prop2Option(w string) Option {
return func(l *Logger) error {
l.Prop1 = w
return nil
}
}
func (l *Logger) SetOptions(opts ...Option) error {
for _, applyOptTo := range opts {
if err := applyOptTo(l); err != nil {
return err
}
}
return nil
}
func testing() {
l := new(Logger)
err := l.SetOptions(Prop1Option(), Prop2Option("world"))
log.Error(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.