【问题标题】:Updating entity in Google Appengine datastore with Go使用 Go 更新 Google Appengine 数据存储中的实体
【发布时间】:2011-10-26 14:46:09
【问题描述】:

我正在玩弄 GAE、Go 和数据存储。我有以下结构:

type Coinflip struct {                                                                                                                                                                                                                      
  Participants []*datastore.Key
  Head         string
  Tail         string
  Done         bool
}

type Participant struct {
  Email string
  Seen  datastore.Time
}

(对于那些想知道我将 Participants 存储为 Key 指针的切片的人,因为 Go 不会自动取消引用实体。)

现在我想找到一个Participant,其特定的Email 地址与已知的Coinflip 相关联。像这样(这有效):

coinflip, _ := find(key_as_string, context)
participants, _ := coinflip.fetchParticipants(context) /* a slice of Participant*/

var found *Participant
for i := 0; i < len(participants) && found == nil; i++ {
  if participants[i].Email == r.FormValue("email") {
    found = &participants[i]
  }
}
(*found).Seen = datastore.SecondsToTime(time.Seconds())

如何将*found 保存到数据存储区?我显然需要密钥,但 Participant 结构和 Key 之间的耦合非常松散。

我不确定如何从这里开始。我还需要从fetchParticipants 调用中返回密钥吗? Java 和 Python GAE 实现看起来相当简单(只需在对象上调用 put())。

提前致谢,

【问题讨论】:

    标签: google-app-engine google-cloud-datastore go


    【解决方案1】:

    我是否还需要从 fetchParticipants 调用中返回密钥?

    是的。然后调用“func Put(c appengine.Context, key *Key, src interface{}) (*Key, os.Error)”

    Java 和 Python GAE 实现看起来相当简单(只是 在对象上调用 put()。

    可能是一个公平的说法。 Go 社区对“魔术”有非常强烈的偏见。在这种情况下,Participant 结构具有您已声明的两个字段。在后台添加密钥将被认为是魔术。

    【讨论】:

      【解决方案2】:

      为了与Go 中的数据进行交互,请考虑将我们的新库https://github.com/matryer/gae-records 用于围绕数据存储的Active Record 数据对象包装器。它为您解决了很多麻烦。

      例如,它支持:

      // create a new model for 'People'
      People := gaerecords.NewModel("People")
      
      // create a new person
      mat := People.New()
      mat.
       SetString("name", "Mat")
       SetInt64("age", 28)
       .Put()
      
      // load person with ID 1
      person, _ := People.Find(1)
      
      // change some fields
      person.SetInt64("age", 29).Put()
      
      // load all People
      peeps, _ := People.FindAll()
      
      // delete mat
      mat.Delete()
      
      // delete user with ID 2
      People.Delete(2)
      
      // find the first three People by passing a func(*datastore.Query)
      // to the FindByQuery method
      firstThree, _ := People.FindByQuery(func(q *datastore.Query){
        q.Limit(3)
      })
      
      // build your own query and use that
      var ageQuery *datastore.Query = People.NewQuery().
        Limit(3).Order("-age")
      
      // use FindByQuery with a query object
      oldestThreePeople, _ := People.FindByQuery(ageQuery)
      
      // using events, make sure 'People' records always get
      // an 'updatedAt' value set before being put (created and updated)
      People.BeforePut.On(func(c *gaerecords.EventContext){
        person := c.Args[0].(*Record)
        person.SetTime("updatedAt", datastore.SecondsToTime(time.Seconds()))
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-12
        • 2013-08-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多