Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google app engine GAE/Go:命名空间不工作_Google App Engine_Go_Google Cloud Datastore - Fatal编程技术网

Google app engine GAE/Go:命名空间不工作

Google app engine GAE/Go:命名空间不工作,google-app-engine,go,google-cloud-datastore,Google App Engine,Go,Google Cloud Datastore,我试图用GAE/Go将数据存储记录存储在namespaceMyNameSpace中,但下面的代码不起作用 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

我试图用GAE/Go将数据存储记录存储在namespace
MyNameSpace
中,但下面的代码不起作用

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
}
此代码在默认命名空间中创建记录,而不是
MyNameSpace

cloud.google.com/go/datastore是否忽略名称空间设置?

我发现

2016年11月8日

打破对数据存储的更改:上下文不再包含名称空间; 相反,您必须显式设置键的命名空间。还有,关键功能 已被更改和重命名

WithNamespace函数已被删除。在中指定命名空间的步骤 查询时,请使用Query.Namespace方法:

q:=datastore.NewQuery(“种类”).Namespace(“ns”)

将导出密钥的所有字段。这意味着您可以使用struct literal构造任何键:

k:=&Key{Kind:“Kind”,ID:37,名称空间:“ns”}

我意识到我应该显式地设置名称空间,但这很不方便。我迁移到使用
google.golang.org/appengine/datastore