布尔值在将实体放入Google App Engine数据存储中时始终为false – Golang

huangapple go评论124阅读模式
英文:

Booleans always false when Putting entities in Google App Engine datastore - Golang

问题

由于某种原因,当我尝试将布尔数据存储在Google Apps数据存储中时,它总是存储为false。

我的实体定义如下:

type Link struct {
  Name          string    //链接的非正式标签。由原始用户设置。
  ...
  isOpen        bool      //告诉我们是否有人可以重写链接。由原始用户设置。
  isPerminant   bool      //告诉我们链接是否应该永久保存。
  isFlagged     bool      //告诉我们内容是否曾被标记为不适当。
}

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

//从表单数据创建Link
l   := Link{
  Name:         r.FormValue("name"),
  ...
  isOpen:       r.FormValue("open")=="on",        
  isPerminant:  r.FormValue("perminant")=="on",
  isFlagged:    r.FormValue("flagged")=="on",
}

我通过运行以下代码来验证数据:

//将Link放入数据存储中
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的输出值为:{[name] ... false false false}

即使我为is[...]属性中的一个硬编码为true值,它们仍然保持为false!为什么?

英文:

For some reason when I attempt to store boolean data in the Google Apps datastore, it always stores as false.

My entity definition looks like this:

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.
}

I create an object and assign values like so:

//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",
}

I verify the data by running the following:

//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}

Even if I hard code in a true value for one of the is[...] properties, they all still remain false! WHHHHHHYYYY???

答案1

得分: 8

尝试将Is中的**I**大写:

type Link struct {
	Name        string //链接的非正式标签。由原始用户设置。
	IsOpen      bool   //告诉我们是否有人可以重写链接。由原始用户设置。
	IsPerminant bool   //告诉我们链接是否应该永久保存。
	IsFlagged   bool   //告诉我们内容是否曾被标记为不适当。
}

.

//从表单数据创建链接
l := Link{
	Name:        r.FormValue("name"),
	IsOpen:      r.FormValue("open") == "on",
	IsPerminant: r.FormValue("perminant") == "on",
	IsFlagged:   r.FormValue("flagged") == "on",
}

要将字段保存到数据存储中,它必须是可导出的。即以大写字母开头。有关更多信息,请阅读Effective Go中的Names部分。

英文:

Try capitalizing the I in 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",
}

For a field to be saved to the datastore it must be exported. I.e. begin with an uppercase letter. For more information please read the Names section of Effective Go

答案2

得分: 0

你是否在硬编码对象之后使用了put方法?确保你所做的任何更改都要跟着put方法,以确保安全。

英文:

Are you using the put method AFTER hardcoding the object? Make sure any changes you make are followed by put, just to be safe.

huangapple
  • 本文由 发表于 2012年7月25日 05:52:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/11639894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定