Last active
December 7, 2019 08:10
-
-
Save wudi/a3a9baa116dd60c17729053b17981fee to your computer and use it in GitHub Desktop.
Functional Options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
type Option func(*Options) | |
type Options struct { | |
Context context.Context | |
Addrs []string | |
Timeout time.Duration | |
} | |
func Timeout(t time.Duration) Option { | |
return func(o *Options) { | |
o.Timeout = t | |
} | |
} | |
// Addrs is the registry addresses to use | |
func Addrs(addrs ...string) Option { | |
return func(o *Options) { | |
o.Addrs = addrs | |
} | |
} | |
// NewRegistry returns a new default registry | |
func NewRegistry(opts ...Option) Registry { | |
options := Options{ | |
Context: context.Background(), | |
Addrs: []string{":8080"}, | |
Timeout: DefaultTimeout, | |
} | |
for _, o := range opts { | |
o(&options) | |
} | |
return mdnsRegistry{Options: options} | |
} | |
r := NewRegistry( | |
Addrs(":8000"), | |
Timeout(10), | |
//... | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment