基本上,您将所有配置函数保持返回主"对象",同时将事物链接在一起并具有最终的"Go"功能或任何您想要调用它以使其执行操作.
这是一个关于游戏的例子
package main
import (
"fmt"
)
func main() {
DoSomething().Go()
DoSomething().WithThisOptionalThing().Go()
}
type Chainable struct {
thing bool
}
func DoSomething() *Chainable {
return &Chainable{}
}
func (c *Chainable) WithThisOptionalThing() *Chainable {
c.thing = true
return c
}
func (c *Chainable) Go() error {
// Actually do something now that it's been configured
fmt.Println("Thing is", c.thing)
return nil
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
84 次 |
| 最近记录: |