广东省住房与城乡建设部网站,wordpress开启多站点功,宁波公司注销,英文网站域名注册目录引入函数选项模式#xff08;functional options pattern#xff09;可选参数默认值接口类型版本引入
假设现在需要定义一个包含多个配置项的结构体#xff0c;具体定义如下#xff1a;
// DoSomethingOption 定义配置项
type DoSomethingOption struct {// a 配置aa…
目录引入函数选项模式functional options pattern可选参数默认值接口类型版本引入
假设现在需要定义一个包含多个配置项的结构体具体定义如下
// DoSomethingOption 定义配置项
type DoSomethingOption struct {// a 配置aa string// b 配置bb string// c 配置cc string// ...
}这个配置结构体中的字段可能有几个也可能有十几个
现在写一个构造函数初始化函数
func NewDoSomethingOption(a,b,c string) *DoSomethingOption {return DoSomethingOption{a: a,b: b,c: c,}
}产生两个问题
如果 DoSomethingOption 有十几个字段构造函数需要定义十几个参数吗如何为某些配置项指定默认值DoSomethingOption 随着业务发展不断新增字段后构造函数是否也需要同步变更变更了构造函数是否又会影响已有代码
函数选项模式functional options pattern
函数选项模式Functional Options Pattern也称为选项模式Options Pattern是一种创造性的设计模式允许使用接受零个或多个函数作为参数的可变构造函数构建复杂结构。
可选参数
Go语言中的函数不支持默认参数但又原生支持可变长参数。
可变长参数的具体类型则需要好好设计一下。它必须满足以下条件
不同的函数参数拥有相同的类型指定函数参数能为特定的配置项赋值支持扩展新的配置项
先定义一个名为OptionFunc的类型它实际上一个接收 *DoSomethingOption 作为参数并且会在函数内部修改其字段的函数。
type OptionFunc func(*DoSomethingOption)接下来我们为DoSomethingOption字段编写一系列WithXxx函数其返回值是一个修改指定字段的闭包函数。
// WithB 将 DoSomethingOption 的 b 字段设置为指定值
func WithB(b string) OptionFunc {return func(o *DoSomethingOption) {o.b b}
}// WithC 将 DoSomethingOption 的 b 字段设置为指定值
func WithC(c string) OptionFunc {return func(o *DoSomethingOption) {o.c c}
}WithXxx是函数选项模式中约定成俗的函数名称格式这样构造函数就可以改写成如下方式了除了必须传递a参数外其他的参数都是可选的。
func NewDoSomethingOption(a string, opts ...OptionFunc) *DoSomethingOption {o : DoSomethingOption{a: a}for _, opt : range opts {opt(o)}return o
}只想传入a和b参数时可以按如下方式
NewDoSomethingOption(德玛西亚, WithB(10))默认值
在使用函数选项模式后可以很方便的为某些字段设置默认值例如下面的示例代码中B默认值为100。
const defaultValueB 100func NewDoSomethingOption(a string, opts ...OptionFunc) *DoSomethingOption {o : DoSomethingOption{a: a, b: defaultValueB} // 字段b使用默认值for _, opt : range opts {opt(o)}return o
}以后要为DoSomethingOption添加新的字段时也不会影响之前的代码只需要为新字段编写对应的With函数即可。
接口类型版本
在一些场景下并不想对外暴露具体的配置结构体而是仅仅对外提供一个功能函数这时可以将对应的结构体定义为小写字母开头将其限制只在包内部使用。
// doSomethingOption 定义一个内部使用的配置项结构体
type doSomethingOption struct {a stringb intc bool// ...
}此时同样是使用函数选项模式但可以通过使用接口类型来“隐藏”内部的逻辑。
// IOption 定义一个接口类型
type IOption interface {apply(*doSomethingOption)
}// funcOption 定义funcOption类型实现 IOption 接口
type funcOption struct {f func(*doSomethingOption)
}func (fo funcOption) apply(o *doSomethingOption) {fo.f(o)
}func newFuncOption(f func(*doSomethingOption)) IOption {return funcOption{f: f,}
}// WithB 将b字段设置为指定值的函数
func WithB(b int) IOption {return newFuncOption(func(o *doSomethingOption) {o.b b})
}// DoSomething 包对外提供的函数
func DoSomething(a string, opts ...IOption) {o : doSomethingOption{a: a}for _, opt : range opts {opt.apply(o)}// 在包内部基于o实现逻辑...fmt.Printf(o:%#v\n, o)
}如此一来我们只需对外提供一个DoSomething的功能函数和一系列WithXxx函数。对于调用方来说使用起来也很方便。
DoSomething(q1mi)
DoSomething(q1mi, WithB(100))