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

孤街醉人 提交于 2019-12-03 11:37:11

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
    }
}

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
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!