在https://github.com/qiniu/qmgo 中 有个方法是传排序参数,通常是一个或多个。
func (q *Query) Sort(fields ...string) QueryI {
if len(fields) == 0 {
// A nil bson.D will not correctly serialize, but this case is no-op
// so an early return will do.
return q
}
var sorts bson.D
for _, field := range fields {
key, n := SplitSortField(field)
if key == "" {
panic("Sort: empty field name")
}
sorts = append(sorts, bson.E{Key: key, Value: n})
}
newQ := q
newQ.sort = sorts
return newQ
}
若需传递多个fields, fields是个可变参数, 传值是你可能想支持多个,但对于参数个数也清楚, 那要先定义个字符串数组。