将实体放入Google App Engine数据存储区时,布尔总是错误的 - Golang

时间:2012-07-24 21:52:49

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

出于某种原因,当我尝试将布尔数据存储在Google Apps数据存储区中时,它始终存储为false。

我的实体定义如下:

type Link struct {
  Name          string    //Coloquial label for link. Set by original User.
  ...
  isOpen        bool      //Tells us whether anyone can rewrite the link. Set by original User.
  isPerminant   bool      //Tells us whether link should be saved forever.
  isFlagged     bool      //Tells us whether the content has ever been flagged inappropriate.
}

我创建一个对象并分配如下值:

//Create Link from form data
l   := Link{
  Name:         r.FormValue("name"),
  ...
  isOpen:       r.FormValue("open")=="on",        
  isPerminant:  r.FormValue("perminant")=="on",
  isFlagged:    r.FormValue("flagged")=="on",
}

我通过运行以下内容来验证数据:

//Put the Link in the datastore
lKey, err := datastore.Put(c, datastore.NewIncompleteKey(c, "Link", nil), &l)
if err != nil {
  http.Error(w, err.Error(), http.StatusInternalServerError)
  return
}

var newLink Link
if err = datastore.Get(c, lKey, &newLink); err != nil {
  http.Error(w, err.Error(), http.StatusInternalServerError)
  return
}

newLink output value: {[name] ... false false false}

即使我为其中一个属性硬编码真值,它们仍然是假的! WHHHHHHYYYY ???

2 个答案:

答案 0 :(得分:8)

尝试在I中大写 Is

type Link struct {
    Name        string //Coloquial label for link. Set by original User.
    IsOpen      bool   //Tells us whether anyone can rewrite the link. Set by original User.
    IsPerminant bool   //Tells us whether link should be saved forever.
    IsFlagged   bool   //Tells us whether the content has ever been flagged inappropriate.
}

//Create Link from form data
l := Link{
    Name:        r.FormValue("name"),
    IsOpen:      r.FormValue("open") == "on",
    IsPerminant: r.FormValue("perminant") == "on",
    IsFlagged:   r.FormValue("flagged") == "on",
}

对于要保存到数据存储区的字段,必须将其导出。即以大写字母开头。有关详细信息,请阅读Effective Go

的名称部分

答案 1 :(得分:0)

您是否在对硬盘进行硬编码后使用put方法?确保您所做的任何更改后面都是put,只是为了安全起见。