[TOC] ## 设计模式 ### 值选项模式 > [参考](https://segmentfault.com/a/1190000013653494) ``` import ( "log" "time" ) type who struct { name string age int timeout time.Duration key int value string } var defaultMyFuncOptions = who{ name: "cpj", age: 0, timeout: time.Second, key: 1, value:"idcpj", } type whoOption func(options *who) func WithOptionAge(age int) whoOption { return func(options *who) { options.age=age } } func WithOptionKeyAndValue(key int, value string) whoOption { return func(options *who) { options.key = key options.value = value } } func NewWho(name string, opts ...whoOption) who { options := defaultMyFuncOptions options.name= name for _, o := range opts { o(&options) } return options } func (w *who) print(){ log.Println(w) } func main() { w := NewWho("idcpj", WithOptionAge(10), WithOptionKeyAndValue(12, "12"), ) w.print()//&{idcpj 10 1000000000 12 12} } ```