Re: [go-nuts] gob register name

715 views
Skip to first unread message

Kyle Lemons

unread,
Mar 3, 2013, 1:01:04 AM3/3/13
to Abhishek Roy, golang-nuts
On Sat, Mar 2, 2013 at 9:31 PM, Abhishek Roy <abhi....@gmail.com> wrote:
Hi

I want to send a concrete struct type (implements an interface) through RPC. The RPC method, on the other side, receives the interface type.

Avoid this.  It has been stated a few times that the fact that it is possible to send interfaces over gob was probably not the best idea.  It is almost always much better to send a concrete type or a value to indicate the concrete type of a subsequent value.
 
In node package:

type InterfaceName interface {
    Action() string   
}

type Approach struct {
  Position int   
}

func (p Approach) Action() string {
  return "Approach"   
}

func (t *RPCMethod) SomeRPCMethod(command InterfaceName, reply *int) error {
// other code
}


In main package:

import "node"

// code snippet for sending over rpc
gob.Register(node.Approach{}) //register node.Approach{}
var p = node.Approach{position}
var q node. InterfaceName = p
var reply int
err = client.Call("RPCMethod. SomeRPCMethod",&q, &reply)

Even after registering node.Approach{} with gob, I am getting the following error message:
gob: name not registered for interface: "node.Approach"

The name must be registered on the decode side.  Thus, it's only possible to send values that the server already knows about.  When that is the case, it's safer and clearer to just arrange to send it type-safely.
 
Any pointers on how to register name of the interface?

Thanks




--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Abhishek Roy

unread,
Mar 3, 2013, 1:21:23 AM3/3/13
to golan...@googlegroups.com, Abhishek Roy
Thanks for the advice Kyle. Registering at the server solved the problem! I was seeing the error message on the client's main method, so did not realize that it was an issue at the server's side.

Now I do agree that sending interfaces over gob was not a good decision. It took almost 5 hours to figure all this out. But coming from Java background it is hard to think any other way.

Thanks for the solution again!

Abhishek
Reply all
Reply to author
Forward
0 new messages