普通网友 2011-10-26 14:46
浏览 4
已采纳

使用Go更新Google Appengine数据存储区中的实体

I am toying with GAE, Go and the datastore. I have the following structs:

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

type Participant struct {
  Email string
  Seen  datastore.Time
}

(For those wondering I store Participants as a slice off Key pointers because Go doesn't automatically dereferences entities.)

Now I want to find a Participant with a particular Email address associated with a know Coinflip. Like so (this works):

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())

How do I save *found to the datastore? I need the key apparently but the coupling between the Participant struct and the Key is very loose.

I'm unsure how to proceed from here. Do I need to return the keys as well from the fetchParticipants call? The Java and Python GAE implementation seem quite a bit simpler (just call put() on the object).

Thanks in advance,

  • 写回答

2条回答 默认 最新

  • dsiimyoc804955 2012-01-27 16:38
    关注

    For interacting with data in Go, consider using our new library https://github.com/matryer/gae-records for an Active Record, data objects wrapper around the datastore. It sorts out a lot of the hassle for you.

    For example, it supports:

    // 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()))
    })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line