I encountered this tricky bug today. After going through all the playground 
examples, I finally understood what was happening.

Similar to Jochen, I was including encoding.MarshalBinary() and 
encoding.UnmarshalBinary() in the interface definition.

A question though. Why does this bug resolve by simply eliminating the 
above two method signatures from the interface definition?

1) My basic understanding was that including these method signatures just 
informs that the underlying concrete type will support these methods. How 
does their inclusion or exclusion change anything?
2) Even if the method signatures are not part of the interface definition, 
the problem of dereferencing a nil-Duck still exists. How does that get 
resolved of methods are excluded in interface definition?

Thanks!

On Friday, August 16, 2019 at 5:45:44 PM UTC+5:30 Jochen Voss wrote:

> Hi Axel,
>
> Thanks a lot for looking into this.  Your post makes things much clearer 
> for me.  In particular, I now think that I probably should not have 
> (Un)MarshalBinary() methods on the interface type, but have them only on 
> the concrete type instead.  For example, this works: 
> https://play.golang.org/p/oBgHqN74ZeM .
>
>
> On Thursday, 15 August 2019 11:16:45 UTC+1, Axel Wagner wrote:
>>
>> The panic, I think, comes from the fact that you pass a *Duck, which then 
>> gets dereferenced, pointing at a nil Duck. Even a nil-Duck *does* have a 
>> DecodeBinary method though, so gob thinks it should use that. It then 
>> type-asserts to BinaryDecoder, which panics, because it's a nil-interface, 
>> just like this example: https://play.golang.org/p/EEwOt0FQunh.
>>
>
> I had forgotten that type assertions don't allow nil values, but indeed 
> they don't.  Thank you for reminding me.
>
> Many thanks,
> Jochen
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/69658e57-1db7-4cf1-b2cb-be66df23637bn%40googlegroups.com.

Reply via email to