Skip to content

Instantly share code, notes, and snippets.

@wudi
Last active December 7, 2019 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wudi/a3a9baa116dd60c17729053b17981fee to your computer and use it in GitHub Desktop.
Save wudi/a3a9baa116dd60c17729053b17981fee to your computer and use it in GitHub Desktop.
Functional Options
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