go|go micro 源码阅读-Options [Functional Options Pattern]

解决的问题 如果想提供一个具有可选参数的方法,那么在其他语言中很简单使用默认值就好了。然而在Go中不支持默认值的做法,退而求其次那么使用函数重载好了,接着就会发现Go中同样不支持函数重载,那么我使用不同的函数名好了。。。。(代码估计很丑,并且不容易辨析)
** Go 不支持默认值参数和函数重载的原因 **

? Method dispatch is simplified if it doesn't need to do type matching as well. Experience with other languages told us that having a variety of methods with the same name but different signatures was occasionally useful but that it could also be confusing and fragile in practice. Matching only by name and requiring consistency in the types was a major simplifying decision in Go's type system.
Regarding operator overloading, it seems more a convenience than an absolute requirement. Again, things are simpler without it
如果不需要做类型匹配的话那么函数分发的处理就会简单。并且具有同样函数名的函数却有不同的类型(增加了代码的理解难度,比如你在读一份代码的时候,看到一个函数调用,然而这个函数调用却有不同的重载或者不同数量的调用参数的话,那么头脑里就要维护这个函数的多个版本,始终不如只存在一个版本理解上来的容易),并且保持函数名和函数类型的一致,可以是Go的类型系统的处理更加简单。
解决方案 以上对Go不支持可选参数和函数重载的原因进行了说明,可是实际的编程中确实有这种模型的需要,如何解决这种问题呢。及如何使用不变的接口,实行可变的构造行为(可变----》 不变的转换)
构造者模式 本身这个一个对象的够着的问题,可以从构造模式的方向进行考虑,显然通过Builder(构造者模式)可以解决这个问题。
可变参数 func Fun(arg ...int)的方式,可是这种方式可变参数的类型只能是统一中类型,那么如果用interface,当然可以,只是函数体内充满了类型断言,对函数本身的逻辑造成了很大的干扰。(职能单一原则)
Functional Options Pattern 与Builder模式类似,变---》不变的转换,找到变动的元素,然后固定他。把变的东西通过特定的设计归一它,把变动的参数类型,归约为不变的函数类型,类似于变换到同类型的可变参数(函数类型)。(及变动的逻辑,只以一种方式存在)
比如说我想制作一杯咖啡,(糖,牛奶,咖啡粉,盐,酒,冰淇淋,蜂蜜。。。。。)这些原料应该是可以选择添加的。那么:一杯 \二/,就可以随意添加自己喜欢的原料了。
type func CoffeeOption (Opts *CoffeeOptions)type CoffeeOptions struct{ sugar int milkint cofferPowder int ... }type Coffee struct{ opts *CoffeeOptions }func CoffeeSugar(sugar int)CoffeeOption{ return func(opts *ConffeeOptions){ opts.sugar = sugar } }func CoffeeMilk(milk int)CoffeeOption{ return func(opts *ConffeeOptions){ opts.milk = milk } }func newDefaultCoffeeOptions()*CoffeeOptions{ return &CoffeeOptions{ sugar:2, milk:5, coffeePowder:100, ..... } }func NewCoffee(opts ...CoffeeOption)*Coffer{ defaultOptions = newDefaultCoffeeOptions() for _,opt := range opts{ opt(defaultOptions) } return &Coffee{ opts:defaultOptions, } }

应用 在go-micro中你可以看到很多functional options pattern的应用,基本每个micro组件都使用了这种方式进行构造。
【go|go micro 源码阅读-Options [Functional Options Pattern]】在grpc中同样很多地方也使用了这种方式。

    推荐阅读