duanan6043 2015-09-03 06:56
浏览 43
已采纳

最初在Python中创建数据存储时,如何在Go中使用数据存储区GAE?

I have a datastore kind "Items" which was created in Python, in this code do not iterate data q.Run() in Go (it's version 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
", x.code)
    }
  • 写回答

1条回答 默认 最新

  • douna1895 2015-09-03 07:05
    关注

    The Datastore package uses reflection to fill struct fields when reading an entity from the datastore. In Go struct fields whose name start with lowercase letter are not exported. Unexported fields cannot be set from packages other than the one they were defined in.

    Only exported fields (that start with uppercase letters) can be stored in / retrieved from the datastore. You can use tags to tell what the name of the property is in the datastore in case it differs from the field's name. So you have to change your Items struct to this:

    type Items struct {
        Code string    `datastore:"code"`
        Date time.Time `datastore:"date"`
        Name string    `datastore:"name"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备