[英]Gob Decoder Returning EOF Error
I am attempting to implement an interface based message queue where jobs are pushed as bytes to a redis queue. 我正在尝试实现基于接口的消息队列,其中作业以字节为单位推送到Redis队列。 But I keep receiving an EOF error when attempting to decode the byte stream.
但是尝试解码字节流时,我一直收到EOF错误。
https://play.golang.org/p/l9TBvcn9qg https://play.golang.org/p/l9TBvcn9qg
Could someone point me in the right direction? 有人可以指出我正确的方向吗?
Thank you! 谢谢!
In your Go Playground example, you're trying to encode an interface and interfaces don't have a concrete implementation. 在您的Go Playground示例中,您尝试对接口进行编码,而接口没有具体的实现。 If you remove the interface from your
A
struct, that should work. 如果您从
A
结构中删除接口,那应该可以。 Like the following: 如下所示:
package main
import "fmt"
import "encoding/gob"
import "bytes"
type testInterface interface{}
type A struct {
Name string
Interface *B // note this change here
}
type B struct {
Value string
}
func main() {
var err error
test := &A {
Name: "wut",
Interface: &B{Value: "BVALUE"},
}
buf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buf)
dec := gob.NewDecoder(buf)
// added error checking as per Mark's comment
err = enc.Encode(test)
if err != nil {
panic(err.Error())
}
result := &A{}
err := dec.Decode(result)
fmt.Printf("%+v\n", result)
fmt.Println("Error is:", err)
fmt.Println("Hello, playground")
}
Also, just as a side note you will see some sort of output like the following: &{Name:wut Interface:0x1040a5a0}
because A
is referencing a reference to a B
struct. 另外,作为旁注,您将看到类似以下的输出:
&{Name:wut Interface:0x1040a5a0}
因为A
引用了对B
结构的引用。 To clean that up further: 要进一步清理,请执行以下操作:
type A struct{
Name string
Interface B // no longer a pointer
}
func main() {
// ...
test := &A{Name: "wut", Interface: B{Value: "BVALUE"}}
// ...
}
Found the answer to the problem from Mark above. 从马克上方找到问题的答案。 I have forgotten to do a
gob.Register(B{})
我忘记做
gob.Register(B{})
https://play.golang.org/p/7rQDHvMhD7 https://play.golang.org/p/7rQDHvMhD7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.