英文:
Datastore object Key field
问题
GAE Datastore API(Go)默认支持返回对象的键吗?
目前,我通过在代码中手动更新一个被忽略的Key
字段来解决这个问题,就像这样进行写入和获取后:
type User struct {
Key *datastore.Key `datastore:"-"`
Email string
...
}
我是否必须这样做,还是有一种方法可以避免这样做?
英文:
Does the GAE datastore API (Go) support returning the key on an object by default?
Currently I'm solving this by updating an ignored Key
field manually in code after writes and fetches like this:
type User struct {
Key *datastore.Key `datastore:"-"`
Email string
...
}
Do I have to do this or is there a way that makes that redundant?
答案1
得分: 1
你必须自己维护该字段。你可以使用反射编写一个函数,在任何结构体上执行该工作,但是在appengine SDK中没有提供你想要的方法。
英文:
You have to maintain that field yourself. You can write a function using reflection which can perform that work on any struct, but there is no method in the appengine SDK to do what you want.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论