type options struct {
PanicWhenInitFail bool
Debug bool
}
type optionFunc func(*options)
func WithPanicWhenInitFail() optionFunc {
return func(o *options) {
o.PanicWhenInitFail = true
}
}
func WithDebug() optionFunc {
return func(o *options) {
o.Debug = true
}
}
func Init(opts ...optionFunc) {
var gOpt = &options{PanicWhenInitFail: false, Debug: false}
for _, opt := range opts {
opt(gOpt)
}
fmt.Println(gOpt)
}
func main() {
Init(WithPanicWhenInitFail(), WithDebug())
}
go灵活修改配置的一种方法,通过遍历函数来修改配置。