gob, pointers, and interface assignment

111 views
Skip to first unread message

Daniël de Kok

unread,
Aug 28, 2015, 5:14:39 AM8/28/15
to golang-nuts
Hi all,

I am running into a problem with gob with some code. A simplified
example is here:

https://play.golang.org/p/hx8A5iqF-p

From the gob documentation:

"Pointers are not transmitted, but the things they point to are
transmitted; that is, the values are flattened. [...] 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, it is clear why decoding does not work: while encoding the only
slice member, the value is flattened. In the gob, Foo is stored and
not *Foo. During decoding, the assignability check fails, because
Frob() is implemented on Foo and not Foo. The decoding problem is
easily fixed by implementing Frob() on Foo.

But what if I actually only want to implement Frob() with a pointer
receiver? Wouldn't it make sense if gob tried if taking the address of
the value would fulfil the interface when the value doesn't fulfil the
interface? Or am I missing something?

Kind regards,
Daniël de Kok

Daniël de Kok

unread,
Aug 31, 2015, 5:42:33 AM8/31/15
to golang-nuts
On Fri, Aug 28, 2015 at 11:13 AM, Daniël de Kok <m...@danieldk.eu> wrote:
> So, it is clear why decoding does not work: while encoding the only
> slice member, the value is flattened. In the gob, Foo is stored and
> not *Foo. During decoding, the assignability check fails, because
> Frob() is implemented on Foo and not Foo.

s/implemented on Foo/implemented on *Foo/
Reply all
Reply to author
Forward
0 new messages