...
函数式选项模式的使用场景有哪些呢:我们一般用来配置一些基础的服务配置,比如MySQL,Redis,Kafka的配置,很多可选参数,可以方便动态灵活的配置想要配置的参数。
https://zhuanlan.zhihu.com/p/436468481
type Option func(*User) func WithAge(age int) Option { return func(u *User) { u.Age = age } } func WithEmail(email string) Option { return func(u *User) { u.Email = email } } func WithPhone(phone string) Option { return func(u *User) { u.Phone = phone } } func WithGender(gender string) Option { return func(u *User) { u.Gender = gender } } func NewUser(id string, name string, options ...func(*User)) (*User, error) { user := User{ ID: id, Name: name, Age: 0, Email: "", Phone: "", Gender: "female", } for _, option := range options { option(&user) } //... return &user, nil } user1, err := NewUser("1", "ada") user2, err := NewUser("2", "bob", WithPhone("123456"), WithGender("male"))
标签:return,string,模式,User,func,go,options,Option From: https://www.cnblogs.com/eiguleo/p/17167162.html