I am trying to decode an Inv struct, but decoding the same encoded value returns a different value.
// inv struct
type Inv struct {
AddrFrom string
Type int
data [][]byte
}
inv := Inv{
AddrFrom: nodeAddress,
Type: kind,
data: inventories,
}
data := GobEncode(inv)
var payload Inv
gob.NewDecoder(bytes.NewBuffer(data)).Decode(&payload)
Here payload and inv have different values. When decoded data field of inv struct is of length zero.
https://pkg.go.dev/encoding/gob
https://go.dev/ref/spec#Exported_identifiers
https://pkg.go.dev/encoding/gob#hdr-Types_and_Values
Internally, the gob package relies on the reflect package, which is designed to respect the visibility principle. Thus the gob package does not handle those fields automatically, it requires you to write dedicated implementation.
https://pkg.go.dev/encoding/gob#GobEncoder
Example
As the field type is already a byte slice, you really are just hitting a visibility access issue and the dedicated required marshalling implementation, while arguable because you could as well export that field, should be straightforward.