Re: [go-nuts] panic: interface conversion: interface is nil, not encoding.BinaryUnmarshaler

2024-02-24 Thread Ninja Ikta
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

Re: [go-nuts] panic: interface conversion: interface is nil, not encoding.BinaryUnmarshaler

2019-08-16 Thread Jochen Voss
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:

Re: [go-nuts] panic: interface conversion: interface is nil, not encoding.BinaryUnmarshaler

2019-08-15 Thread 'Axel Wagner' via golang-nuts
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

Re: [go-nuts] panic: interface conversion: interface is nil, not encoding.BinaryUnmarshaler

2019-08-15 Thread 'Axel Wagner' via golang-nuts
Hm, I looked a bit at the code. ISTM that if you implement BinaryMarshaler/Unmarshaler (or GobEncoder/Decoder), the values are sent as a []byte, not an interface, so the concrete type information is lost. You can kind of circumvent this, by instead passing the values as interface{}:

Re: [go-nuts] panic: interface conversion: interface is nil, not encoding.BinaryUnmarshaler

2019-08-15 Thread 'Axel Wagner' via golang-nuts
On Thu, Aug 15, 2019 at 11:14 AM Jochen Voss wrote: > 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. > `Register` only gets the

Re: [go-nuts] panic: interface conversion: interface is nil, not encoding.BinaryUnmarshaler

2019-08-15 Thread Jochen Voss
Hi Axel, On Thursday, 15 August 2019 09:13:17 UTC+1, Axel Wagner wrote: > > 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

Re: [go-nuts] panic: interface conversion: interface is nil, not encoding.BinaryUnmarshaler

2019-08-15 Thread 'Axel Wagner' via golang-nuts
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

Re: [go-nuts] panic: interface conversion: interface is nil, not encoding.BinaryUnmarshaler

2019-08-15 Thread Jochen Voss
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

Re: [go-nuts] panic: interface conversion: interface is nil, not encoding.BinaryUnmarshaler

2019-08-14 Thread Marcin Romaszewicz
Here you go: https://play.golang.org/p/_cPmaSxRatC You want to unmarshal into , not into This means: var duck2 A not: var duck2 Duck On Wed, Aug 14, 2019 at 8:46 AM Jochen Voss wrote: > Hello, > > I'm trying to read gob-encoded data into a variable of interface type. In > simple cases,