Created
October 17, 2020 04:36
-
-
Save podhmo/3bd3bea46a3849daa7b20f63e609e9c1 to your computer and use it in GitHub Desktop.
This file contains 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 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