Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created October 17, 2020 04:36
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 podhmo/3bd3bea46a3849daa7b20f63e609e9c1 to your computer and use it in GitHub Desktop.
Save podhmo/3bd3bea46a3849daa7b20f63e609e9c1 to your computer and use it in GitHub Desktop.
package main
type Config struct {
Port int
MaxConns int
}
func (c *Config) Apply(target *Config) {
target.Port = c.Port
target.MaxConns = c.MaxConns
}
type ServerOpt interface {
Apply(*Config)
}
type ServerOptFunc func(*Config)
func (f ServerOptFunc) Apply(c *Config) {
f(c)
}
func WithPort(port int) ServerOpt {
return ServerOptFunc(func(c *Config) {
c.Port = port
})
}
func NewServer(host string, options ...ServerOpt) *Server {
c := &Config{
Port: 8080,
MaxConns: 1,
}
for _, opt := range options {
opt(c)
}
return &Server{
Host: host,
Port: c.Port,
MaxConns: c.MaxConns,
}
}
type Server struct {
Host string
Port int
MaxConns int
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment