The encoder delivers a stream that includes type information. If you
reuse the stream to a different decoder, that decoder will not have
seen the type information sent to the first one.

Or to look at it another way, the encoder just sends the values you
give it, along with any type information that has not already been
transmitted. In effect, it can't tell the difference between calling
it twice to deliver to one encoder and calling it twice to deliver to
two decoders.

Don't reuse the encoder.

-rob

On Mon, Nov 29, 2021 at 7:51 PM cpu...@gmail.com <cpui...@gmail.com> wrote:
>
> Turns out that the decoding problem only exist when I reuse the encoder:
>
> b := new(bytes.Buffer)
> enc := gob.NewEncoder(b)
>
> for param := range in {
> b.Reset()
> if err := enc.Encode(&param.Val); err != nil {
> panic(err)
> }
>
> // send encoded data
> }
>
> When I create a new encode inside the loop things are fine. I couldn't find 
> anything regarding enc that should prevent reusing it?
>
> Cheers,
> Andi
> On Sunday, November 28, 2021 at 9:10:37 PM UTC+1 cpu...@gmail.com wrote:
>>
>> I'm trying to transfer arbitrary structs over gRPC. To achieve this I've 
>> trying to use gob for encoding the interface{} part of the structs. The code 
>> looks somewhat like https://go.dev/play/p/4Q5CQE4Wcy2 but not entirely.
>>
>> Two things strike me strange:
>> - I receive "gob: decoding into local type []float64, received remote type 
>> []float" during Decode when the interface is a []float64
>> - I need to use &res.Val (which is an interface) or it will stay nil. Using 
>> res.Val (which imho works with JSON when the target is an interface) does 
>> always lead to nil result.
>>
>> I would appreciate any hint how to gRPC or gob.Encode interfaces properly.
>>
>> Cheers,
>> Andi
>>
> --
> 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/589e4ba5-afe4-4cee-9398-326478171492n%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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOXNBZRnFNujCrmHKBt_RS3%2BVHBf6Rm1VUQvaDKVKEQopQPcxA%40mail.gmail.com.

Reply via email to