1

A continuation from this question:

Doing a "IN Array" query on google app engine datastore with golang

Right now, I am following the suggestion from the previous question on querying with an array of keys/ids ids []int64. These IDs may or may not actually exist (they have been deleted, but the reference on other instances have not been removed).

My method of trying to obtain these instances looks like so:

var keys []*datastore.Key

for _, id := range ids {
    keys = append(keys, datastore.NewKey(c, "Category", "", id, nil))
}

categories := make([]Category, len(keys))
err := datastore.GetMulti(c, keys, categories)
if err != nil {
    return nil, err
}

for i := 0; i < len(categories); i++ {
    categories[i].Id = keys[i].IntID()
}

However, it errors out throwing me:

datastore: no such entity

I could on the other hand grab each one individually, but is there a more efficient way to approach this?

Community
  • 1
  • 1
Steven Lu
  • 2,150
  • 1
  • 24
  • 33

1 Answers1

2

You need to type assert the error to an appengine.MultiError. This way you can get access to the errors for an individual entity.

if me, ok := err.(appengine.MultiError); ok {
    for i, e := range me {
        // e != nil if entity i failed
    }
} else {
   // something else went wrong (timeout, etc).
}

See the docs for MultiError here

Logiraptor
  • 1,496
  • 10
  • 14
  • Thanks! Looks like I was searching for the wrong thing. Searching for `MultiError` instantaneously found me solutions. http://stackoverflow.com/questions/15252521/gae-go-how-to-use-getmulti-with-non-existent-entity-keys – Steven Lu Mar 27 '15 at 20:18