亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

這個函數在哪里返回非錯誤?

這個函數在哪里返回非錯誤?

Go
達令說 2021-11-15 15:48:12
我正在圍繞數據存儲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錯誤。


查看完整回答
反對 回復 2021-11-15
  • 1 回答
  • 0 關注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號