Created
February 18, 2015 13:34
-
-
Save hogedigo/c62ea6ac71d7ad84c1b8 to your computer and use it in GitHub Desktop.
GetMutiでentityがなかった場合
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
MultiErrorに入ってるErrNoSuchEntityをチェックしてやらないといけなくてちょー面倒(´・ω・`)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
結果