数据存储对象的键字段

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

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.

huangapple
  • 本文由 发表于 2015年5月28日 00:49:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/30488629.html
匿名

发表评论

匿名网友

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

确定