我正在围绕数据存储GetAll函数构建一个包装器,我很难找到该函数返回非错误的位置。在我看来,除非有任何其他错误(即当它中断时Done),否则它会返回errFieldMismatch我认为不正确的。func (q *Query) GetAll(c context.Context, dst interface{}) ([]*Key, error) { var ( dv reflect.Value mat multiArgType elemType reflect.Type errFieldMismatch error ) if !q.keysOnly { dv = reflect.ValueOf(dst) if dv.Kind() != reflect.Ptr || dv.IsNil() { return nil, ErrInvalidEntityType } dv = dv.Elem() mat, elemType = checkMultiArg(dv) if mat == multiArgTypeInvalid || mat == multiArgTypeInterface { return nil, ErrInvalidEntityType } } var keys []*Key for t := q.Run(c); ; { k, e, err := t.next() if err == Done { break } if err != nil { return keys, err } if !q.keysOnly { ev := reflect.New(elemType) if elemType.Kind() == reflect.Map { // This is a special case. The zero values of a map type are // not immediately useful; they have to be make'd. // // Funcs and channels are similar, in that a zero value is not useful, // but even a freshly make'd channel isn't useful: there's no fixed // channel buffer size that is always going to be large enough, and // there's no goroutine to drain the other end. Theoretically, these // types could be supported, for example by sniffing for a constructor // method or requiring prior registration, but for now it's not a // frequent enough concern to be worth it. Programmers can work around // it by explicitly using Iterator.Next instead of the Query.GetAll // convenience method. x := reflect.MakeMap(elemType) ev.Elem().Set(x) }
1 回答
慕娘9325324
TA贡献1783条经验 获得超4个赞
默认是 return errFieldMismatch
,它在函数顶部声明,但未初始化。
如果ErrFieldMismatch
在迭代过程中的任何时候都没有,errFieldMismatch
仍然会nil
在最后,因此GetAll
会返回nil
错误。
- 1 回答
- 0 关注
- 150 浏览
添加回答
举报
0/150
提交
取消