简体   繁体   English

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

[英]Update Entity Appengine Datastore with Go

I'm trying to find an effective example in how to perform updates on appengine datastore with Go. 我正在尝试找到一个有效的例子来说明如何使用Go对appengine数据存储区执行更新。 All the examples I've found on the web are very vague and mostly explains concepts and not the "real life". 我在网上找到的所有例子都非常模糊,主要解释概念而不是“现实生活”。 The appengine documentation for go says: go的appengine文档说:

..."Updating an existing entity is a matter of performing another Put() using the same key." ......“更新现有实体是使用相同的密钥执行另一个Put()的问题。”

My problem here is being in how to retrieve the key. 我的问题在于如何检索密钥。 So I have the code below to store and retrieve data: 所以我有下面的代码来存储和检索数据:

func subscribe(w http.ResponseWriter, r *http.Request) {

    user := User {
        Name: r.FormValue("username"),
        Email: r.FormValue("useremail"),
        Flag: 0,
    }

    c := appengine.NewContext(r)
    //datastore.Put(c, datastore.NewIncompleteKey(c, "User", nil), &user)
    datastore.Put(c, datastore.NewKey(c, "User", "stringID", 0, nil), &user)

    template.Must(template.ParseFiles("confirmation.html")).Execute(w, nil)

}

func checkusers(w http.ResponseWriter, r *http.Request) {

    c := appengine.NewContext(r)

    qUsers := datastore.NewQuery("User")

    var users []User

    qUsers.GetAll(c, &users)

    template.Must(template.ParseFiles("users.html")).Execute(w, users)
}

How do I do an update on the flag property changing its value tom 1? 如何更新标志属性更改其值tom 1?

I'm a bit confused on this thing as I couldn't fully understand how the "key" is stored for each entity. 我对这件事感到有点困惑,因为我无法完全理解每个实体如何存储“密钥”。

Any help would be very appreciated. 任何帮助将非常感激。

todo an update you first need to identify if your object is a new or an old one. 您首先需要确定您的对象是新对象还是旧对象。 this can be simple done by adding the following method to your User struct: 这可以通过向User结构添加以下方法来简单地完成:

type User struct {
    Name string
    Email string
    Flag int64  `datastore:"-"`
}
func (u *User) IsNew() bool {
    return u.Flag == 0
}

this tells data-store to ignore the Flag field when storing and retrieving an object and because initial value of int64 is zero, a newly created object can be identified if Flag is zero 这告诉数据存储在存储和检索对象时忽略Flag字段,并且因为int64的初始值为零,如果Flag为零,则可以识别新创建的对象

so creating a new object just needs to set UserName and Email: 所以创建一个新对象只需要设置UserName和Email:

user := User {
    Name: r.FormValue("username"),
    Email: r.FormValue("useremail")
}

then next step is to either use a IncompleteKey or a Key, for the put statement 然后下一步是为put语句使用IncompleteKey或Key

could look like this: 可能看起来像这样:

var k *datastore.Key
if user.IsNew() {
    k = datastore.NewIncompleteKey(c, "Users", nil)
} else {
    k = datastore.NewKey(c, "Users", "", user.Flag, nil)
}
k, err := datastore.Put(c, k, user)
if err != nil {
    return k, err
}

with an incomplete key, app-engine will generate a new key for you. 使用不完整的密钥,app-engine将为您生成一个新密钥。
after put you can assign the new key to your object: 放完之后,您可以将新密钥分配给您的对象:

user.Flag = k.IntID

now if you do a query later you need to assign the Id to your query result objects, the query will return the keys of query result in the same order so you can change your code like this: 现在,如果稍后进行查询,则需要将Id分配给查询结果对象,查询将以相同的顺序返回查询结果的键,以便您可以像这样更改代码:

keys, err := q.GetAll(c, &users)
if err != nil {
    return
}
l := len(users)
for i := 0; i < l; i++ {
    users[i].Flag = keys[i].IntID()
}

thats all, for more information, just have a look a the reference document there is explained with methods return which values. 多数民众赞成所有,有关更多信息,只需查看参考文档,解释方法返回哪些值。
https://developers.google.com/appengine/docs/go/datastore/reference https://developers.google.com/appengine/docs/go/datastore/reference

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM