--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/b19c51bc-524c-4292-915b-fc00e9289052%40googlegroups.com.
Here you go:You want to unmarshal into &A, not into &DuckThis means:var duck2 Anot:var duck2 Duck
On Wed, Aug 14, 2019 at 8:46 AM Jochen Voss <joche...@gmail.com> wrote:
Hello,--I'm trying to read gob-encoded data into a variable of interface type. In simple cases, this works for me, but when I use a custom encoding via MarshalBinary() and UnmarshalBinary() methods, my code keeps crashing, with the error messagepanic: interface conversion: interface is nil, not encoding.BinaryUnmarshalerExample:- The code at https://play.golang.org/p/y8nWNhObUwb, letting gob do the en-/decoding works.- The very similar code at https://play.golang.org/p/-zS7QjEJg9x, which uses MarshalBinary() and UnmarshalBinary() crashes with the panic shown above.What am I doing wrong?[I asked the same question at https://stackoverflow.com/questions/57485698/, with no answers so far]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 golan...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/042b28c0-d768-4cba-94cf-159cf4231818%40googlegroups.com.
I haven't really used gob much, so unfortunately I can't *really* help you. But one thing to note is that you can dump the content of the buffer and see that it doesn't actually contain the name of the type you are encoding: https://play.golang.org/p/R8HB6RP8kS0
And I agree that this probably shouldn't panic, but instead return an error. But given that the encoded bytes don't contain the name of the concrete struct, there is no way the decoder can know where you want to put it, without giving it the correct type.
You are right. But the type is somehow represented by some index or so, instead of the name? Since the type needs to be registered using gob.Register() beforehand, this seems not totally impossible to me.
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. (A nil interface value is identified by the empty string and transmits no value.) Upon receipt, the decoder verifies that the unpacked concrete item satisfies the interface of the receiving variable.
But then, when I change your code to let gob do the encoding itself, the name is suddenly there: https://play.golang.org/p/Qz7zDKL047z, so probably the absence of the type name is part of the problem.
And I agree that this probably shouldn't panic, but instead return an error. But given that the encoded bytes don't contain the name of the concrete struct, there is no way the decoder can know where you want to put it, without giving it the correct type.Yes, an informative error instead of the hard to interpret panic would be nice. I wonder whether this is indeed intended behaviour, or whether this is some kind of bug in the encoding/gob package.
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/32c27653-c50b-42cd-a525-8c9e9298bf60%40googlegroups.com.
-----Original Message-----
From: 'Axel Wagner' via golang-nuts
Sent: Aug 15, 2019 3:12 AM
To: Jochen Voss
Cc: golang-nuts
Subject: Re: [go-nuts] panic: interface conversion: interface is nil, not encoding.BinaryUnmarshaler
I haven't really used gob much, so unfortunately I can't *really* help you. But one thing to note is that you can dump the content of the buffer and see that it doesn't actually contain the name of the type you are encoding: https://play.golang.org/p/R8HB6RP8kS0
--
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 golan...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/b19c51bc-524c-4292-915b-fc00e9289052%40googlegroups.com.
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/042b28c0-d768-4cba-94cf-159cf4231818%40googlegroups.com.
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfFp567QVvMy4iOmYfuWzQFdbNovNAJXGu%2Bk_%3DcDKm7yBg%40mail.gmail.com.
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.