google app engine - Get entity by id -
i using gorilla mux routes , pass id.
using id how can entity datastore.
param := mux.vars(r) c := appengine.newcontext(r) item := []item{} pr, err := datastore.newquery("item").filter("id = ", param["id"]).getall(c, &item)
and here i'm stuck, tried using filter doesn't work.
what have next?
you don't if using integer or string ids entities. i'll assume integer because requires little more code. first, create key:
n, err := strconv.parseint(param["id"], 10, 64) if err != nil { // handle error } key := datastore.newkey(c, "item", "", n, nil)
now have key, can fetch entity:
var item item if err := datastore.get(c, key, &item); err != nil { // handle error }
it's more efficient get entity query entity. if want query id, see key filters in app engine docs.
google-app-engine go
No comments:
Post a Comment