Gob interface question / Confusion with gob.Register()

771 views
Skip to first unread message

Brendan Tracey

unread,
Feb 25, 2013, 10:12:09 PM2/25/13
to golan...@googlegroups.com
Hi,

I'm trying to use Gob to save some data structures. It seems to be really clean, which is very nice. However, I'm getting an error involving interfaces, and I'm not sure how to fix the issue. The relevant piece of code is http://play.golang.org/p/daRidNhjuL (Though the actual code is more complicated)


Running it like this, I get the error, 
gob: local interface type *nnet.reshaper can only be decoded from remote interface type; received concrete type sigmoidneuron = struct { }

The gob package doc says "Interface values are transmitted as a string identifying the concrete type being sent (a name that must be pre-defined by calling Register), followed by a byte count of the length of the following data (so the value can be skipped if it cannot be stored), followed by the usual encoding of concrete (dynamic) value stored in the interface value."

I also found a "working as intended" issue which gives the code http://play.golang.org/p/FO3hNdzRkL

Is there a way to dynamically register the underlying types of the interface value as I encode them? I tried

gob.Register(neur.reshaper)

gob.Register(& neur.reshaper)

gob.Register(reflect.ValueOf(neur.reshaper))

and

gob.Register(reflect.TypeOf(neur.reshaper))

None of which work. It even did not work when I added 

    gob.Register(&sigmoidneuron{})

    gob.Register(&linearneuron{})

To all of the places where gob is active as in

http://play.golang.org/p/3WHq789HtH

I'm confused. Thanks

Steven Blenkinsop

unread,
Feb 25, 2013, 11:10:19 PM2/25/13
to Brendan Tracey, golang-nuts
http://play.golang.org/p/vDde_rjoPc

See lines 39 and 57. 57 is just registering the concrete type. The problem on line 39 was this:

var i interface{} = neur.reshaper

i is now an interface{} containing a *sigmoidneuron. There's no way to know that the value assigned to i was a reshaper. If you want to preserve the interface, you have to use a pointer to it:

var i interface{} = &neur.reshaper

i is now an interface{} containing a *reshaper which points to a reshaper containing a *sigmoidneuron.


--
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.
 
 

Brendan Tracey

unread,
Feb 25, 2013, 11:50:27 PM2/25/13
to golan...@googlegroups.com, Brendan Tracey
Ahhh, that makes sense. Works great now, thanks!
Reply all
Reply to author
Forward
0 new messages