GAE Go — How to use GetMulti with non-existent entity keys?

后端 未结 2 1879
星月不相逢
星月不相逢 2021-02-13 18:40

I\'ve found myself needing to do a GetMulti operation with an array of keys for which some entities exist, but some do not.

My current code, below, returns

2条回答
  •  猫巷女王i
    2021-02-13 19:21

    GetMulti can return a appengine.MultiError in this case. Loop through that and look for datastore.ErrNoSuchEntity. For example:

    if err := datastore.GetMulti(c, keys, dst); err != nil {
        if me, ok := err.(appengine.MultiError); ok {
            for i, merr := range me {
                if merr == datastore.ErrNoSuchEntity {
                    // keys[i] is missing
                }
            }
        } else {
            return err
        }
    }
    

提交回复
热议问题