Skip to content

Instantly share code, notes, and snippets.

@hogedigo
Created February 18, 2015 13:34
Show Gist options
  • Save hogedigo/c62ea6ac71d7ad84c1b8 to your computer and use it in GitHub Desktop.
Save hogedigo/c62ea6ac71d7ad84c1b8 to your computer and use it in GitHub Desktop.
GetMutiでentityがなかった場合
package getmultitest
import (
"appengine"
"appengine/datastore"
"fmt"
"net/http"
)
func init() {
http.HandleFunc("/", handler)
}
type Test struct {
Value string
}
func handler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
key1 := datastore.NewKey(c, "Test", "1", 0, nil)
key2 := datastore.NewKey(c, "Test", "2", 0, nil)
key3 := datastore.NewKey(c, "Test", "3", 0, nil)
if _, err := datastore.Put(c, key1, &Test{"test1"}); err != nil {
http.Error(w, err.Error(), 500)
return
}
if _, err := datastore.Put(c, key3, &Test{"test3"}); err != nil {
http.Error(w, err.Error(), 500)
return
}
keys := []*datastore.Key{key1, key2, key3}
values := make([]Test, 3)
if err := datastore.GetMulti(c, keys, values); err != nil {
if merr, ok := err.(appengine.MultiError); ok {
fmt.Fprintf(w, "%v¥n", keys)
fmt.Fprintf(w, "%v¥n", values)
fmt.Fprintf(w, "%#v¥n", merr)
}
}
}
@hogedigo
Copy link
Author

結果

[/Test,1 /Test,2 /Test,3]
[{test1} {} {test3}]
appengine.MultiError{error(nil), (*errors.errorString)(0xc21000abe0), error(nil)}

@hogedigo
Copy link
Author

MultiErrorに入ってるErrNoSuchEntityをチェックしてやらないといけなくてちょー面倒(´・ω・`)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment