我正在尝试保存两条记录,然后获取第二条记录。问题是过滤器似乎不起作用。虽然我按姓名(“Andrew W”)过滤,但我总是得到“Joe Citizen”。计数器还指示 2 条记录,而它应该只有 1 条记录。这让我发疯。请参阅下面的完整代码。结果打印counter 2 e2 {"Joe Citizen" "Manager" "2015-03-24 09:08:58.363929 +0000 UTC" ""}
package main
import (
"fmt"
"time"
"net/http"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
)
type Employee struct {
Name string
Role string
HireDate time.Time
Account string
}
func init(){
http.HandleFunc("/", handle)
}
func handle(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
e1 := Employee{
Name: "Joe Citizen",
Role: "Manager",
HireDate: time.Now(),
}
_, err := datastore.Put(c, datastore.NewKey(c, "employee", "", 0, nil), &e1)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
panic(err)
return
}
e1.Name = "Andrew W"
_, err = datastore.Put(c, datastore.NewKey(c, "employee", "", 0, nil), &e1)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
panic(err)
return
}
var e2 Employee
q := datastore.NewQuery("employee")
q.Filter("Name =", "Andrew W")
cnt, err := q.Count(c)
if err !=nil{
http.Error(w, err.Error(), http.StatusInternalServerError)
panic(err)
return
}
for t := q.Run(c); ; {
if _, err := t.Next(&e2); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
panic(err)
return
}
break
}
fmt.Fprintf(w, "counter %v e2 %q", cnt, e2)
}
aluckdog
相关分类