我正在圍繞數(shù)據(jù)存儲(chǔ)GetAll函數(shù)構(gòu)建一個(gè)包裝器,我很難找到該函數(shù)返回非錯(cuò)誤的位置。在我看來,除非有任何其他錯(cuò)誤(即當(dāng)它中斷時(shí)Done),否則它會(huì)返回errFieldMismatch我認(rèn)為不正確的。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貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊
默認(rèn)是 return errFieldMismatch
,它在函數(shù)頂部聲明,但未初始化。
如果ErrFieldMismatch
在迭代過程中的任何時(shí)候都沒有,errFieldMismatch
仍然會(huì)nil
在最后,因此GetAll
會(huì)返回nil
錯(cuò)誤。
- 1 回答
- 0 關(guān)注
- 178 瀏覽
添加回答
舉報(bào)
0/150
提交
取消