options模式
const (
DefaultHost = "127.0.0.1"
DefaultPort = "8080"
)
type Option func(c *Client)
type Client struct {
Host string
Port string
}
func NewClient(options ...Option) *Client {
c := &Client{
Host: DefaultHost,
Port: DefaultPort,
}
for _, option := range options {
option(c)
}
return c
}
func WitchHost(host string) Option {
return func(c *Client) {
c.Host = host
}
}