Please see upthread - gob does that for you. On Thu, Aug 15, 2019 at 5:59 PM Robert Engels <reng...@ix.netcom.com> wrote:
> I think you will need to encode a struct that contains the type name and > the struct. Then you decode using reflection. > > -----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 > I agree that from the documentation, it seems to me that it *should* and I > can't really see what this is doing wrong. 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. > > On Thu, Aug 15, 2019 at 9:22 AM Jochen Voss <jochen.v...@gmail.com> wrote: > >> Dear Marcin, >> >> My aim is to unmarshal into an interface variable, without having to >> know in advance which concrete type I'm receiving (the actual interface has >> several possible implementations, and gob data comes in over a network >> connection). So, while your code avoids the panic, it does not solve my >> problem. >> >> Many thanks, >> Jochen >> >> On Wednesday, 14 August 2019 22:12:54 UTC+1, Marcin Romaszewicz wrote: >>> >>> Here you go: >>> https://play.golang.org/p/_cPmaSxRatC >>> >>> You want to unmarshal into &A, not into &Duck >>> >>> This means: >>> var duck2 A >>> >>> not: >>> 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 message >>>> >>>> panic: interface conversion: interface is nil, not >>>> encoding.BinaryUnmarshaler >>>> >>>> Example: >>>> >>>> - 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 view this discussion on the web visit >>>> https://groups.google.com/d/msgid/golang-nuts/b19c51bc-524c-4292-915b-fc00e9289052%40googlegroups.com >>>> <https://groups.google.com/d/msgid/golang-nuts/b19c51bc-524c-4292-915b-fc00e9289052%40googlegroups.com?utm_medium=email&utm_source=footer> >>>> . >>>> >>> -- >> 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/042b28c0-d768-4cba-94cf-159cf4231818%40googlegroups.com >> <https://groups.google.com/d/msgid/golang-nuts/042b28c0-d768-4cba-94cf-159cf4231818%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- > 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/CAEkBMfFp567QVvMy4iOmYfuWzQFdbNovNAJXGu%2Bk_%3DcDKm7yBg%40mail.gmail.com > <https://groups.google.com/d/msgid/golang-nuts/CAEkBMfFp567QVvMy4iOmYfuWzQFdbNovNAJXGu%2Bk_%3DcDKm7yBg%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > > > > > -- 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/CAEkBMfGZ6FynU7ps2hSK-bXYsUYy5p%3DNEHF3reZcVh3%2BdwzDMQ%40mail.gmail.com.