1

我有以下基于教程的代码,但是每当我读取我的值时,它们的值都为零。我不知道为什么,我在 Put 调用之前检查了结构,并且值很好。

type StartReport struct {
    reportTime time.Time
    userID int64
}
func startHandler(w http.ResponseWriter, r *http.Request) {
    query := r.URL.RawQuery
    queries := strings.Split(query, "&")
    for _, q := range queries {
        pair := strings.Split(q, "=")
        key := pair[0]
        id := pair[1]
        if key == "uid" {
            c := appengine.NewContext(r)
            c.Infof("User %v reported app start: %v", id, humanTime(time.Now()))
            idNum, err := strconv.ParseInt(id, 0, 64)
            if err != nil {
                reportHttpError(c, w, err)
                return
            }
            startReport := StartReport{time.Now(), idNum}
            c.Debugf("startReport - userID: %v, reportTime: %v", startReport.userID, humanTime(startReport.reportTime))
            _, err = datastore.Put(c, datastore.NewIncompleteKey(c, "start report", nil), &startReport)
            if err != nil {
                reportHttpError(c, w, err)
            } else {
                fmt.Fprintf(w, "USER %v - start report OK", id)
            }
        } else {
            fmt.Fprintf(w, "Improper request: %v", r.URL)
        }
    }
}

func readDatastoreHandler(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    q := datastore.NewQuery("start report")
    var startReports []*StartReport
    if _, err := q.GetAll(c, &startReports); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    } else {
        out := "<html><body><h1>DATA STORE RECORDS:</h1><h3>Start Reports:</h3>"
        for i, report := range startReports {
            c.Debugf("StartReport[%d]: user: %v, time: %v", i, report.userID, report.reportTime)
            out += fmt.Sprintf("<p> [ userID: %v - time: %v ] <p>", report.userID, report.reportTime)
        }
        out += "</body></html>"
        fmt.Fprintf(w, out)
    }
}
4

0 回答 0