1 回答

TA贡献2021条经验 获得超8个赞
至少在 Golang 中,getter 的使用是一种反模式。这种期权模式是众所周知的。Setter 和 getter 在 Golang 空间中并不常见。
此选项模式有一个不错的好处,您可以将多个选项函数传递给您的构建器或构造函数,然后遍历所有传递的选项以修改此选项类型,就像在您的示例中一样
// then. we build a new object, we just need to receive a list of option
func NewObject(options ...Option) Object {
final := &Options{}
// then apply each option to options
for _, option := range options {
option(final)
}
// then build an object based on the final object
}
构造函数调用示例:
NewObject(optionA, optionB, optionC, optionD)
吸气剂和二传手
https://golang.org/doc/effective_go.html#Getters
你肯定读过有效的围棋指南 -> https://golang.org/doc/effective_go.html
- 1 回答
- 0 关注
- 135 浏览
添加回答
举报