python - 最初是在 Python 中创建的,如何在 Go 中使用数据存储 GAE?

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

我有一个用 Python 创建的数据存储类型 "Items",在这段代码中不要在 Go 中迭代数据 q.Run()(它是版本 2) :

type Items struct{
    code string
    date time.Time
    name string
}
func getcode(w http.ResponseWriter, r *http.Request) {
    code := mux.Vars(r)["code"]
    fmt.Fprintf(w,"get code %v",code)

    c := appengine.NewContext(r)
    q := datastore.NewQuery("Items")

    for t := q.Run(c); ; {
        var x Items
        key, err := t.Next(&x)
        fmt.Fprintf(w,"%v",key)

        if err == datastore.Done {
            break
        }
        if err != nil {
            //serveError(c, w, err)
            return
        }
        fmt.Fprintf(w, "Code=%v\n", x.code)
    }

最佳答案

Datastore 包在从数据存储区读取实体时使用反射来填充结构字段。在 Go 中,名称以小写字母开头的结构字段不会被导出。未导出的字段不能从定义它们的包以外的包中设置。

只有导出的字段(以大写字母开头)可以存储在数据存储中/从数据存储中检索。您可以使用标签来告知属性在数据存储中的名称,以防它与字段名称不同。因此,您必须将 Items 结构更改为:

type Items struct {
    Code string    `datastore:"code"`
    Date time.Time `datastore:"date"`
    Name string    `datastore:"name"`
}

关于python - 最初是在 Python 中创建的,如何在 Go 中使用数据存储 GAE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32368888/

相关文章:

python-2.7 - app_identity_service.GetAccessToken() 需要比可用配额更多的配额

go - 是否可以在 golang 中连接到 cloudsql?

cpu 的 golang no/debug/pprof/profile 端点,同时拥有其他一切

python - 在没有 Internet 访问的情况下安装 Python 模块

python - celery worker 队列

java - 在每个请求上获取内存缓存变量会导致问题

java - 将 Java 应用程序移植到 Go - 有什么建议吗?

python - Fabric 无法在函数中导入模块

python - 字符串操作似乎效率低下

python - 使用 Google AppEngine 中的 Gmail API