英文:
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:"name" datastore:"-"`
Difficulty int `json:"difficulty"`
}
When only using a load property it also works.
func (s *Trick) Load(c <-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<- 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<- datastore.Property) error {
defer close(c)
c <- datastore.Property{
Name: "Difficulty",
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<- datastore.Property) error {
defer close(c)
return datastore.SaveStruct(s, c)
}
The following is ok
func (s *Trick) Save(c chan<- datastore.Property) error {
return datastore.SaveStruct(s, c)
}
This is also ok
func (s *Trick) Save(c chan<- datastore.Property) error {
defer close(c)
c <- datastore.Property{
Name: "Difficulty",
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论