Gob cannot work with pointers in a slice

1,342 views
Skip to first unread message

Eric Z

unread,
Oct 22, 2013, 1:08:05 PM10/22/13
to golan...@googlegroups.com
Hello Everyone,

I've encountered a problem when I was trying to serialize pointers in a slice using gob package in golang. I tried to follow the example in http://blog.golang.org/gobs-of-data

The original code defines two struts

type P struct {
    X, Y, Z int
    Name    string
}

type Q struct {
    X, Y *int32
    Name string
}

Encode P and Decode Q works great for me. However, when I try to encode []P and decode []Q. It won't work unless I revise the two pointers X, Y *int32 as X, Y int32.

How can I solve this problem? I have put the error code here http://play.golang.org/p/MTmMb-1DdJ

If you change X, Y *int32 in the above code to X, Y int32. It will work but from my understanding of reflection, both should work with Gob.

Thank you very much for help.

Cheers,
Eric

Rob Pike

unread,
Oct 22, 2013, 1:36:16 PM10/22/13
to Eric Z, golan...@googlegroups.com
It's working as intended. You're possibly confused for two reasons.

1) You're printing pointers using %d, which will print the pointer address as a decimal number.
2) The first entry in the array has a zero, which gob will not send, so the corresponding pointer on the receiver is never allocated.

Point 2 has caused confusion for others. It's a perhaps unfortunate consequence of the way zero values are handled, but it's there.

This version works without trouble: http://play.golang.org/p/lwA7ejTYmp


-rob

Eric Z

unread,
Oct 22, 2013, 2:09:49 PM10/22/13
to golan...@googlegroups.com, Eric Z
Hello Rob,

Thank you very much for the help. It solves my problem. It seems like I have check the zero values for serialization format upgrades.

Cheers,
Eric

Dan Kortschak

unread,
Oct 22, 2013, 6:12:27 PM10/22/13
to Rob Pike, golan...@googlegroups.com
On Tue, 2013-10-22 at 10:36 -0700, Rob Pike wrote:
> Point 2 has caused confusion for others. It's a perhaps unfortunate
> consequence of the way zero values are handled, but it's there.
>
What's the recommended approach to serialising pointer fields where the
nil state versus the zero state of the value pointed to is significant?
For bool I've used Gob{Encoder,Decoder}s to map the states to ints
[0,2], but this is less nice for string, float or int (maybe map to
struct{bool; string}, struct{bool; int} or struct{bool; float}?).

Dan

Rob Pike

unread,
Oct 22, 2013, 7:04:15 PM10/22/13
to Dan Kortschak, golan...@googlegroups.com
There is no recommended solution. It's an unintended consequence of the design that would be nice to address if it can be done well.

Dan Kortschak

unread,
Oct 22, 2013, 7:15:33 PM10/22/13
to Rob Pike, golan...@googlegroups.com
Yes, I understand the reasoning behind it - I've had this discussion
with Nigel Tao[1]. I was wondering what a reasonable work around is for
the current state of encoding/gob.

[1] thread starting at
https://groups.google.com/d/msg/golang-nuts/Q5sn8851sc4/BmqdcCE-cdEJ

Rob Pike

unread,
Oct 22, 2013, 7:21:20 PM10/22/13
to Dan Kortschak, golan...@googlegroups.com
The safest is to write your own transcoder for the relevant type. Otherwise, check for nil.

I do not claim the situation is satisfactory.

-rob

Reply all
Reply to author
Forward
0 new messages