我的数据存储属性加载器(PropertyLoadSaver)在Golang中不起作用。

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

My datastore PropertyLoadSaver does not work in golang

问题

我使用的文档(https://developers.google.com/appengine/docs/go/datastore/reference#hdr-The_PropertyLoadSaver_Interface)

只使用这个结构体而不使用propertyLoadSaver,一切正常。

type Trick struct {
    Name string `json:"name" datastore:"-"`
    Difficulty int `json:"difficulty"`
}

只使用load属性也可以正常工作。

func (s *Trick) Load(c <-chan datastore.Property) error {
    return datastore.LoadStruct(s, c)
}

使用save属性会导致appengine在返回行处发生panic?

func (s *Trick) Save(c chan<- datastore.Property) error {
    defer close(c)
    return datastore.SaveStruct(s, c)
}

如果我尝试使用以下代码,它不会导致panic,但也不会保存任何内容?

func (s *Trick) Save(c chan<- datastore.Property) error {
    defer close(c)
    c <- datastore.Property{
        Name:  "Difficulty",
        Value: s.Difficulty,
    }
    return nil
}

(使用sdk v1.9.8)

英文:

The documentation I used (https://developers.google.com/appengine/docs/go/datastore/reference#hdr-The_PropertyLoadSaver_Interface)

Just using this struct without using the propertyLoadSaver everything works.

type Trick struct {
	Name string `json:&quot;name&quot; datastore:&quot;-&quot;`
	Difficulty int `json:&quot;difficulty&quot;`
}

When only using a load property it also works.

func (s *Trick) Load(c &lt;-chan datastore.Property) error {
    return datastore.LoadStruct(s, c)
}

The save property results into a appengine panic at the return line?

func (s *Trick) Save(c chan&lt;- datastore.Property) error {
    defer close(c)
    return datastore.SaveStruct(s, c)
}

When I try this instead it will not result into a panic but does not save anything?

func (s *Trick) Save(c chan&lt;- datastore.Property) error {
    defer close(c)
    c &lt;- datastore.Property{
        Name:  &quot;Difficulty&quot;,
        Value: s.Difficulty,
    }
    return nil
}

(Using sdk v1.9.8)

答案1

得分: 3

这里的defer close(c)会引发 panic。

func (s *Trick) Save(c chan<- datastore.Property) error {
    defer close(c)
    return datastore.SaveStruct(s, c)
}

以下是正确的写法:

func (s *Trick) Save(c chan<- datastore.Property) error {
    return datastore.SaveStruct(s, c)
}

这个也是正确的写法:

func (s *Trick) Save(c chan<- datastore.Property) error {
    defer close(c)
    c <- datastore.Property{
        Name:  "Difficulty",
        Value: int64(s.Difficulty),
    }
    return nil
}

(请编辑文档以使其更清晰 https://developers.google.com/appengine/docs/go/datastore/reference#hdr-The_PropertyLoadSaver_Interface

英文:

Here defer close(c) will cause a panic

func (s *Trick) Save(c chan&lt;- datastore.Property) error {
    defer close(c)
    return datastore.SaveStruct(s, c)
}

The following is ok

func (s *Trick) Save(c chan&lt;- datastore.Property) error {
    return datastore.SaveStruct(s, c)
}

This is also ok

func (s *Trick) Save(c chan&lt;- datastore.Property) error {
    defer close(c)
	c &lt;- datastore.Property{
        Name:  &quot;Difficulty&quot;,
        Value: int64(s.Difficulty),
    }
	return nil
}

(please edit the documentation to make this more clear https://developers.google.com/appengine/docs/go/datastore/reference#hdr-The_PropertyLoadSaver_Interface)

huangapple
  • 本文由 发表于 2014年8月3日 05:21:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/25099525.html
匿名

发表评论

匿名网友

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

确定