.groupBy("name")
.selectKey()
.selectCount()
.orderByDescending("count")
.ofType(ReduceResult.class)
.toList();
链式调用仅在经过异常停止错误交互的言语中有效。当一个函数额外前往一个错误,就没法向下面那样停止链式调用。
为了在 Go 中复制链式调用,我运用了一个“形状错误(stateful error)”的办法:
type Query struct {
err error
}
func (q *Query) WhereEquals(field string, val interface{}) *Query {
if q.err != nil {
return q
}
// logic that might set q.err
return q
}
func (q *Query) GroupBy(field string) *Query {
if q.err != nil {
return q
}
// logic that might set q.err
return q
}
func (q *Query) Execute(result inteface{}) error {
if q.err != nil {
return q.err
}
// do logic
}
链式调用可以这么写:
var result *Foo
(责任编辑:admin)