سؤال

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?

هل كانت مفيدة؟

المحلول 2

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.

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top