英文:
name not registered for interface
问题
我正在尝试通过RPC发送一个具体的实现。RPC方法期望一个接口。
相关的代码片段如下:
在node
包中:
type Commander interface {
Action() string
}
type Approach struct {
Position int
}
func (p Approach) Action() string {
return "Approach"
}
func (t *RPCMethod) RPCAction(command Commander, reply *int) error {
// RPC方法
}
在main
包中:
import "node"
gob.Register(node.Approach{})
var p = node.Approach{position}
var q node.Commander = p
var reply int
err = client.Call("RPCMethod.RPCAction",&q, &reply)
我已经使用gob注册了node.Approach
。但是在运行主程序时,我收到了以下错误信息:
gob: name not registered for interface: "node.Approach"
对我做错了什么有任何想法吗?或者如何注册名称?
英文:
I am trying to send a concrete implementation over RPC. The RPC methods expects a interface.
The relevant code snippets are:
In package node
:
type Commander interface {
Action() string
}
type Approach struct {
Position int
}
func (p Approach) Action() string {
return "Approach"
}
func (t *RPCMethod) RPCAction(command Commander, reply *int) error {
// RPC Method
}
In package main
:
import "node"
gob.Register(node.Approach{})
var p = node.Approach{position}
var q node.Commander = p
var reply int
err = client.Call("RPCMethod.RPCAction",&q, &reply)
I have registered the node.Approach with the gob. But on running the main program I am receiving
gob: name not registered for interface: "node.Approach"
Any ideas on what I am doing wrong? Or how to register name?
答案1
得分: 4
是的,你已经使用gob注册了node.Approach
。但是你传递的是q
,它不是node.Approach
。应该传递p
,因为它具有你已经注册的类型。
英文:
Yes, you have registered node.Approach
with the gob. But then you pass q
, which is not node.Approach
. Send p
instead, because that has the type you have registered.
答案2
得分: 1
如我在邮件列表上回答的那样,您需要在解码端注册类型;在这种情况下,即RPC服务器端。gob编码器使用反射来检查结构和接口,但解码端必须事先知道可能存储在接口内的类型,因为通过网络传输的只是名称,而且它需要先看到类型。
英文:
As I answered on the mailing list, you will need to register the type on the decoding side; in this case, that is the RPC server side. The gob encoder is using reflection to examine the structures and interfaces, but the decoder side must know up front what types might be stored inside an interface because the only thing transmitted over the wire is the name, and it needs to have already seen the type first.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论