2

出于某种原因,当我尝试在 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}

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

4

2 回答 2

8

尝试大写Iin 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的名称部分

于 2012-07-25T01:49:54.373 回答
0

您是否在硬编码对象后使用 put 方法?为了安全起见,请确保您所做的任何更改都跟在 put 之后。

于 2012-07-24T22:08:11.947 回答