我最近在玩 AppEngine 并遇到以下代码的问题,其中似乎写入数据存储的唯一值是日期。我花了一段时间对此感到震惊,发现只需将结构中的第一个字符大写即可修复它!
我想知道是否还有其他人遇到过这个问题并且知道为什么我无法在 Golang 结构中为数据存储使用小写成员名称?我认为这可能是 Google AppEngine 处理结构写入方式的错误。
这是问题代码:
package main
import (
"fmt"
"net/http"
"time"
"appengine"
"appengine/datastore"
)
/* This is the problem struct */
type storeVal struct {
firstName string //FirstName works
lastName string //LastName works
email string //Email works
password string //Password works
Date time.Time
}
func init() {
http.HandleFunc("/", handle)
http.ListenAndServe(":8080", nil)
}
func handle(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
/* Write values to DataStore */
e1 := storeVal{
firstName: "Bob", //FirstName works
lastName: "Smith", //lastName works
email: "bob.smith@test.com", //Email works
password: "password!", //Password works
Date: time.Now(),
}
key := datastore.NewIncompleteKey(c, "storeVal", nil)
_, err := datastore.Put(c, key, &e1)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "successfully added {firstName: '%s', lastName: '%s', email: '%s', password: '%s'}", e1.firstName, e1.lastName, e1.email, e1.password)
/* Read values back in. */
q := datastore.NewQuery("storeVal").
Filter("firstName =", "Bob").
Filter("lastName =", "Smith").
Order("-Date")
var storeVals []storeVal
_, err2 := q.GetAll(c, &storeVals)
if err2 != nil {
http.Error(w, err2.Error(), http.StatusInternalServerError)
return
}
if (len(storeVals) == 0) {
fmt.Printf("No results.");
return
}
明月笑刀无情
相关分类