返回EOF错误的Gob解码器

返回EOF错误的Gob解码器,go,gob,Go,Gob,我试图实现一个基于接口的消息队列,其中作业作为字节推送到redis队列。但在尝试解码字节流时,我一直收到EOF错误 有人能给我指出正确的方向吗 谢谢大家! 在Go Playerd示例中,您试图对接口进行编码,但接口没有具体的实现。如果从Astruct中删除接口,应该可以正常工作。例如: package main import "fmt" import "encoding/gob" import "bytes" type testInterface interface{} type A s

我试图实现一个基于接口的消息队列,其中作业作为字节推送到redis队列。但在尝试解码字节流时,我一直收到EOF错误

有人能给我指出正确的方向吗


谢谢大家!

在Go Playerd示例中,您试图对接口进行编码,但接口没有具体的实现。如果从
A
struct中删除接口,应该可以正常工作。例如:

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")
}
另外,正如旁注一样,您将看到如下某种输出:
和{Name:wut Interface:0x1040a5a0}
,因为
a
引用的是对
B
结构的引用。为了进一步清理这一问题:

type A struct{
  Name string
  Interface B // no longer a pointer
}

func main() {
   // ...
   test := &A{Name: "wut", Interface: B{Value: "BVALUE"}}
   // ...
}

在Go Playerd示例中,您试图对接口进行编码,但接口没有具体的实现。如果从
A
struct中删除接口,应该可以正常工作。例如:

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")
}
另外,正如旁注一样,您将看到如下某种输出:
和{Name:wut Interface:0x1040a5a0}
,因为
a
引用的是对
B
结构的引用。为了进一步清理这一问题:

type A struct{
  Name string
  Interface B // no longer a pointer
}

func main() {
   // ...
   test := &A{Name: "wut", Interface: B{Value: "BVALUE"}}
   // ...
}

从上面的标记中找到了问题的答案。我忘了做一个
gob.Register(B{})


从上面的标记中找到了问题的答案。我忘了做一个
gob.Register(B{})


enc.Encode(test)函数返回一个错误,捕获并检查所有可能的错误是一个很好的做法,谢谢提醒。我通常会捕获并处理错误,但在本例中,我只是试图演示对嵌套接口进行编码和解码时出现的问题。对不起,我的意思是调用返回错误,当运行代码并捕获错误时,它会显示“gob:type not registed for interface:main.B”。在编码之前添加一个gob.Register(B{})可以防止EOF。我完全忘记了必须注册!谢谢你,马克!我会注意检查错误。“问题和答案应该包含所有相关代码,并且不依赖第三方网站(Go Playder链接很好,但它们不应该是理解问题的唯一方式)。enc.Encode(测试)函数返回一个错误,捕获并检查所有可能的错误是一个很好的做法。我通常会捕获并处理错误,但在本例中,我只是试图演示嵌套接口的编码和解码问题。对不起,我的意思是调用返回一个错误,当运行代码并捕获错误时,它显示“gob:type not registed for interface:main.B”。在编码之前添加一个gob.Register(B{})可以防止EOF。我完全忘记了必须注册!谢谢标记!我会注意检查错误。问题和答案应该包括所有相关代码,并且不依赖第三方网站(Go Playerd链接很好,但它们不应该是理解问题的唯一方式)。我使用接口来抽象作业队列,这样我就不必每次都将case assert切换到特定的结构类型。(除了B之外,还有许多结构实现了testInterface)这是否意味着我无法将子字段抽象到接口中?太好了!很高兴知道这一点。我本人没有使用过
gob
包。我使用过其他
编码
包,所以我认为这是相同的行为。我使用接口来抽象作业队列,这样我就不必将case assert转换为每次都有特定的结构类型。(除了B之外,还有许多结构实现testInterface)这是否意味着我无法将子字段抽象到接口中?太好了!很高兴知道。我本人没有使用过
gob
包。我使用过其他
编码
包,所以我认为这是相同的行为。