如何在使用Go语言的Google App Engine中通过键获取实体

huangapple go评论79阅读模式
英文:

how to get entity by key in google app engine using go language

问题

现在我使用Go语言在Datastore中添加了一些实体,我可以查询所有实体,但我想通过键获取一个实体,如何获取实体的键并按键查询,就像这样:select * from Blog where key=?

c := appengine.NewContext(r)
g := Blog{
Title: r.FormValue("title"),
Content: r.FormValue("content"),
}
_, err := datastore.Put(c, datastore.NewIncompleteKey(c, "Blog", nil), &g)

英文:

now I add some entity in datastore using Go language,
I can query all entity, but I want get a entity by key,
how to get entity'key and query by key like this: select * from Blog where key=?

c := appengine.NewContext(r)
g := Blog{
Title: r.FormValue("title"),
    Content: r.FormValue("content"),
}
_, err := datastore.Put(c, datastore.NewIncompleteKey(c, "Blog", nil), &g)

Thanks

答案1

得分: 2

查看Go Runtime的Datastore文档

var b Blog
err := datastore.Get(c, key, &b)
英文:

See the Datastore documentation for the Go Runtime:

var b Blog
err := datastore.Get(c, key, &b)

huangapple
  • 本文由 发表于 2012年6月3日 11:34:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/10867689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定