我最近重组了我的代码,现在main包下有两个包:chain和api.
在chain我定义了一些 structsSomeStruct1和这些结构SomeStruct2的接口SomeInterface。以下是chain/cli.go外观。
package chain
type CLI struct{}
func (cli *CLI) Run() {
...
gob.Register(SomeStruct1{})
gob.Register(SomeStruct2{})
...
}
我放的api/api.go里面还有另一个类似的地方。Run()gob.Register(chain.SomeStruct1{})
main.go看起来像这样:
package main
import (
"myproj/api"
"myproj/chain"
)
func main() {
// I have also tried the following lines.
// gob.Register(chain.SomeStruct1{})
// gob.Register(chain.SomeStruct2{})
go api.Run()
cli := chain.CLI{}
cli.Run()
}
但是,我gob: name not registered for interface: "main.SomeStruct1"在运行时遇到了错误。当我将所有代码都放在一个包中时,这并没有发生main,我觉得奇怪的SomeStruct1是现在在chain包下但错误提到了main.SomeStruct1. 我哪里弄错了gob.Register()?