Search code examples
google-app-enginegoogle-cloud-datastorego

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


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 an error (datastore: no such entity).

err := datastore.GetMulti(c, keys, infos)

So how can I do this? I'd use a "get or insert" method, but there isn't one.


Solution

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