选项模式
package mainimport "fmt"type Options struct {StrOption1 stringStrOption2 stringStrOption3 stringIntOption1 intIntOption2 intIntOption3 int}func InitOptions1(strOption1 string, strOption2 string, strOption3 string,IntOption1 int, IntOption2 int, IntOption3 int) {options := &Options{}options.StrOption1 = strOption1options.StrOption2 = strOption2options.StrOption3 = strOption3options.IntOption1 = IntOption1options.IntOption2 = IntOption2options.IntOption3 = IntOption3fmt.Printf("init options1:%#v\n", options)return}func InitOptions2(opts ...interface{}) {options := &Options{}for index, opt := range opts {switch index {case 0:str, ok := opt.(string)if !ok {return}options.StrOption1 = strcase 1:str, ok := opt.(string)if !ok {return}options.StrOption2 = strcase 2:str, ok := opt.(string)if !ok {return}options.StrOption3 = strcase 3:val, ok := opt.(int)if !ok {return}options.IntOption1 = valcase 4:val, ok := opt.(int)if !ok {return}options.IntOption2 = valcase 5:val, ok := opt.(int)if !ok {return}options.IntOption3 = val}}fmt.Printf("init options2:%#v\n", options)}type Option func(opts *Options)func InitOption3(opts ...Option) {options := &Options{}for _, opt := range opts {opt(options)}fmt.Printf("init options3:%#v\n", options)}func WithStringOption1(str string) Option {return func(opts *Options) {opts.StrOption1 = str}}func WithStringOption2(str string) Option {return func(opts *Options) {opts.StrOption2 = str}}func WithStringOption3(str string) Option {return func(opts *Options) {opts.StrOption3 = str}}func WithIntOption1(val int) Option {return func(opts *Options) {opts.IntOption1 = val}}func WithIntOption2(val int) Option {return func(opts *Options) {opts.IntOption2 = val}}func WithIntOption3(val int) Option {return func(opts *Options) {opts.IntOption3 = val}}func main() {InitOptions1("str1", "str2", "str3", 1, 2, 3)InitOptions2("str1", "str2", "str3", 1, 2, 3)InitOption3(WithStringOption1("str1"),WithStringOption3("str3"),WithIntOption3(3),WithIntOption2(2),WithIntOption1(1),WithStringOption2("str2"),)}
