doutun1362 2015-09-08 12:17
浏览 30
已采纳

如何使用Go从Google App Engine中的模型中删除字段?

What is the Go equivalent of Python delattr or Java: Entity.removeProperty?

I am trying to remove a property from the datastore as described here: removing deleted properties from the datastore

  • 写回答

1条回答 默认 最新

  • dpy3846 2015-09-08 14:03
    关注

    In order to remove a property from a saved entity, you have to load it first, and save it again with the same key but without the property you want to remove. If you want to remove a property from all saved entities (of a kind), you have to load and save each one-by-one. (Of course you may use other means like Query's and datastore.PutMulti() to query and save multiple entities.)

    You can remove a property from a saved entity in basically 2 ways:

    Using 2 structs:

    You can use 2 structs: your old model and the new model (without the property you want to remove):

    type Old struct {
        Name     string `datastore:"name"`
        Removeme string `datastore:"removeme"`
    }
    
    type New struct {
        Name     string `datastore:"name"`
    }
    

    And load the entity and re-save it (with the same key):

    c := appengine.NewContext(r)
    // Constructing the key, for example:
    k := datastore.NewKey(c, "Entity", "stringID", 0, nil)
    
    e := new(Old)
    if err = datastore.Get(c, key, e); err != nil {
        // Datastore error.
        return
    }
    
    e2 := New{e.Name}
    if _, err = datastore.Put(c, k, &e2); err != nil {
        // Datastore error
    }
    

    Using PropertyList

    Or you can use the datastore.PropertyList to load any entity into it.

    It's basically just a slice of Property's:

    type PropertyList []Property
    

    Remove the property (or properties) you want to delete from this slice, and re-save it with the same key.

    Basically the steps are the same: load an entity with a key, remove unwanted properties, and resave it (with the same key).

    Remove an element from a slice:

    To delete the element from slice a at index i:

    a = append(a[:i], a[i+1:]...)
    // or
    a = a[:i+copy(a[i:], a[i+1:])]
    

    So basically it looks something like this:

    c := appengine.NewContext(r)
    // Constructing the key, for example:
    k := datastore.NewKey(c, "Entity", "stringID", 0, nil)
    
    e := datastore.PropertyList{}
    if err = datastore.Get(c, key, &e); err != nil {
        // Datastore error.
        return
    }
    
    // Loop over the properties to find the one we want to remove:
    for i, v := range e {
        if v.Name == "removeme" {
            // Found it!
            e = append(e[:i], e[i+1:]...)
            break
        }
    }
    
    if _, err = datastore.Put(c, k, &e); err != nil {
        // Datastore error
    }
    

    Note: Be careful when removing multiple elements from a slice using for range. Result might be unexpected as when you remove an element, all subsequent elements are shifted and you may skip an element. For details see this answer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?