英文:
gob: type not registered for interface: fasthbbftv1.BroadcastMessage
问题
我的代码组织结构如下:
package_name/
server/
rpc_server.go
client_test.go
- 我注册了一个RPC服务器来接收其他节点的消息。
type RPCMessageArgs struct {
Operation string
Info Message
}
type Message struct {
NodeID uint64
Payload interface{}
}
func init() {
gob.Register(BroadcastMessage{})
}
func main() {
// 启动RPC服务器
}
- 我使用命令
go run rpc_server.go
在server文件夹中启动了RPC服务器。 - 我在client_test.go中的测试代码如下:
package package_name
import (
"encoding/gob"
"log"
"net/rpc"
"strconv"
"testing"
)
type BroadcastMessage struct {
Payload interface{}
}
func TestClient(t *testing.T) {
gob.Register(BroadcastMessage{})
args := &RPCMessageArgs{}
args.Info.NodeID = 3
args.Info.Payload = &BroadcastMessage{}
reply := &RPCMessageReply{}
client, err := rpc.Dial("tcp", ":800"+strconv.Itoa(1))
if err != nil {
log.Fatal("dialing:", err)
}
err = client.Call("RPCServer.RemoteCall", args, reply)
if err != nil {
log.Fatal("call error:", err)
}
}
- 我遇到了以下错误:
call error: gob: name not registered for interface: "package_name.BroadcastMessage"
英文:
My code organization structure is like this
package_name/
server/
rpc_server.go
client_test.go
- I register an RPC server to receive messages from other nodes.
type RPCMessageArgs struct {
Operation string
Info Message
}
type Message struct {
NodeID uint64
Payload interface{}
}
func init() {
gob.Register(BroadcastMessage{})
}
func main() {
// start rpc server
}
- I start my RPC server in the server folder with the command
go run rpc_server.go
. - My test code in the client_test.go is as follows
package package_name
import (
"encoding/gob"
"log"
"net/rpc"
"strconv"
"testing"
)
type BroadcastMessage struct {
Payload interface{}
}
func TestClient(t *testing.T) {
gob.Register(BroadcastMessage{})
args := &RPCMessageArgs{}
args.Info.NodeID = 3
args.Info.Payload = &BroadcastMessage{}
reply := &RPCMessageReply{}
client, err := rpc.Dial("tcp", ":800"+strconv.Itoa(1))
if err != nil {
log.Fatal("dialing: ", err)
}
err = client.Call("RPCServer.RemoteCall", args, reply)
if err != nil {
log.Fatal("call error: ", err)
}
}
- I get this error
call error: gob: name not registered for interface: "package_name.BroadcastMessage"
答案1
得分: 0
我犯了一个非常低级的错误。我在两个文件中定义了两个type BroadcastMessage
,一个在rpc_server.go
中,一个在client_go.go
中。我应该在一个包中定义这些结构,然后让这两个文件调用这些结构。
改变后,我的代码结构如下:
package_name/
args/
rpc_args.go
server/
rpc_server.go
client_test.go
在rpc_args.go
文件中定义相关结构,在其他文件中导入args
包,然后在rpc_server.go
和client_test.go
中调用gob.Register()
命令以正常运行。
英文:
I made a very low-level mistake. I defined two type BroadcastMessage
in two files, one in rpc_server.go
and one in client_go.go
. I should define these structures in a package, and then let these two files call these structures.
After the change, my code structure is as follows:
package_name/
args/
rpc_args.go
server/
rpc_server.go
client_test.go
Define the relevant structure in the rpc_args.go file, import the args package in other files, and then call the gob.Register()
command in rpc_server.go
and client_test.go
to run normally.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论