我正在圍繞數據存儲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 關注
- 192 瀏覽
添加回答
舉報
0/150
提交
取消