猿问

这个函数在哪里返回非错误?

我正在围绕数据存储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)

            }



达令说
浏览 151回答 1
1回答

慕娘9325324

默认是 return errFieldMismatch,它在函数顶部声明,但未初始化。如果ErrFieldMismatch在迭代过程中的任何时候都没有,errFieldMismatch仍然会nil在最后,因此GetAll会返回nil错误。
随时随地看视频慕课网APP

相关分类

Go
我要回答