dongsigan2636 2017-04-21 01:18
浏览 46
已采纳

GAE / Go:命名空间不起作用

I am trying to store datastore record in namespace MyNameSpace with GAE/Go, but the code below not working.

import (
    "cloud.google.com/go/datastore"
    "github.com/gin-gonic/gin"
    "google.golang.org/appengine"
)

func Save(c *gin.Context, list []MyStruct) ([]MyStruct, error) {
    r := c.Request
    ctx := appengine.NewContext(r)
    ctx_with_namespace, err := appengine.Namespace(ctx, "MyNameSpace")
    if err != nil {
        return nil, err
    }

    client, err := datastore.NewClient(ctx_with_namespace, "MyProject")
    if err != nil {
        return nil, err
    }

    var keyList []*datastore.Key
    for _, v := range list {
        key := datastore.NameKey("MyStruct", v.ColA, nil)
        keyList = append(keyList, key)
    }

    _, err = client.PutMulti(ctx_with_namespace, keyList, list)

    return list,nil
}

This code creates records in the default namespace, not MyNameSpace.

Does cloud.google.com/go/datastore ignores namespace setting?

  • 写回答

1条回答 默认 最新

  • donqo88682 2017-04-21 01:45
    关注

    I found this document

    November 8, 2016

    Breaking changes to datastore: contexts no longer hold namespaces; instead you must set a key's namespace explicitly. Also, key functions have been changed and renamed.

    The WithNamespace function has been removed. To specify a namespace in a Query, use the Query.Namespace method:

    q := datastore.NewQuery("Kind").Namespace("ns")

    All the fields of Key are exported. That means you can construct any Key with a struct literal:

    k := &Key{Kind: "Kind", ID: 37, Namespace: "ns"}

    I realized I should explicitly set namespace, but it is very inconvenient. I migrated to use google.golang.org/appengine/datastore

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

报告相同问题?

悬赏问题

  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题