如何使用go lang在appengine中设置命名空间?

时间:2017-09-18 14:41:50

标签: google-app-engine go google-cloud-datastore

我能够插入具有默认命名空间的实体,但我需要实现多租户。下面是我用于插入实体并获取实体的代码,但我需要为每个实体分配名称空间。所以我已经按照下面的链接但我无法设置名称空间请帮我修复

https://cloud.google.com/appengine/docs/standard/go/multitenancy/multitenancy

我发现有两个包

1.“cloud.google.com/go/datastore” - 通过使用此软件包,我可以插入带有名称空间的实体 2.“google.golang.org/appengine/datastore” - 我发现这个包来自谷歌的多租户链接, 我得到了清晰度,我需要使用此包来分配命名空间但我在使用此包时遇到错误,请帮我解决这个问题

package main

import (
    "fmt"
    "net/http"

    "google.golang.org/appengine"
    "google.golang.org/appengine/datastore"
)

type user struct {
    UserName string
    Password string
    First    string
    Last     string
}

func main() {
    fmt.Println("hi")
    http.HandleFunc("/", handle)
    http.Handle("/favicon.ico", http.NotFoundHandler())
    http.ListenAndServe(":8080", nil)

}

func handle(w http.ResponseWriter, r *http.Request) {
    ctx := appengine.NewContext(r)

    k := datastore.NewKey(ctx, "user", "stringID", 0, nil)
    e := new(user)
    if err := datastore.Get(ctx, k, e); err != nil {
        http.Error(w, err.Error(), 500)
        return
    }

    old := e.First
    e.First = r.URL.Path

    if _, err := datastore.Put(ctx, k, e); err != nil {
        http.Error(w, err.Error(), 500)
        return
    }

    w.Header().Set("Content-Type", "text/plain; charset=utf-8")
    fmt.Fprintf(w, "old=%q\nnew=%q\n", old, e.First)
}

错误:

While running the code from local host I am getting below error

E:/GoWork/src/google.golang.org/appengine/internal/api.go:270 +0x186
google.golang.org/appengine.WithContext(0xd60000, 0xc04200c320, 0xc042174000, 0xd60000, 0xc04200c320)
E:/GoWork/src/google.golang.org/appengine/appengine.go:73 +0x46
google.golang.org/appengine.NewContext(0xc042174000, 0x987460, 0xc04216c034)
E:/GoWork/src/google.golang.org/appengine/appengine.go:66 +0x6e
main.handle(0x959da0, 0xc042180000, 0xc042174000)
E:/GoWork/src/simplystdatastore/main.go:27 +0x4a 
net/http.HandlerFunc.ServeHTTP(0x7e49c8, 0x959da0, 0xc042180000, 0xc042174000)
c:/go/src/net/http/server.go:1942 +0x4b
net/http.(*ServeMux).ServeHTTP(0x987460, 0x959da0, 0xc042180000, 0xc042174000)
c:/go/src/net/http/server.go:2238 +0x137
net/http.serverHandler.ServeHTTP(0xc042075550, 0x959da0, 0xc042180000, 0xc042174000)
c:/go/src/net/http/server.go:2568 +0x99
net/http.(*conn).serve(0xc0421280a0, 0x95a3a0, 0xc04216e040)
c:/go/src/net/http/server.go:1825 +0x619
created by net/http.(*Server).Serve
c:/go/src/net/http/server.go:2668 +0x2d5 

0 个答案:

没有答案