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

后端 未结 2 1864
星月不相逢
星月不相逢 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条回答
  •  执念已碎
    2021-02-13 19:19

    I know this topic is up for more than a few days, but I like to post an alternative, using type switch.

    if err := datastore.GetMulti(c, keys, dst); err != nil {
      switch errt := err.(type) {
      case appengine.MultiError:
        for ix, e := range errt {
          if e == datastore.ErrNoSuchEntity {
            // keys[ix] not found
          } else if e != nil {
            // keys[ix] have error "e"
          }
        }
      default:
        // datastore returned an error that is not a multi-error
      }
    }
    

提交回复
热议问题