I have recently restructured my code so that now under the main
package there are two packages: chain
and api
.
In chain
I defined a few structs SomeStruct1
, SomeStruct2
and an interface SomeInterface
for those structs. The following is what chain/cli.go
looks like.
package chain
type CLI struct{}
func (cli *CLI) Run() {
...
gob.Register(SomeStruct1{})
gob.Register(SomeStruct2{})
...
}
There is another similar api/api.go
where inside Run()
I put gob.Register(chain.SomeStruct1{})
.
main.go
looks like this:
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()
}
However, I got the error gob: name not registered for interface: "main.SomeStruct1"
at runtime. This did not happen when I had all the code inside one main
package and I felt weird that SomeStruct1
is now under chain
package but the error referred to main.SomeStruct1
. Where did I get wrong of gob.Register()
?
I have not been able to solve the problem completely and I think the cause was that chain.SomeStruct1
was somehow recognized at runtime as having the name main.SomeStruct1
while it was registered with the internal name main.SomeStruct1
.
Therefore a workaround I have now is using gob.RegisterName("main.SomeStruct1", chain.SomeStruct1)
.