You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What steps will reproduce the problem?
1. Try to encode, then decode a []*Object where Object.Children = []*Object
What is the expected output?
No error
What do you see instead? In Decoder.Decode: "gob: wrong type ([]*main.Object) for
received field .Children"
(because Object.Children[0].Children is nil?)
Which compiler are you using (5g, 6g, 8g, gccgo)? 6g
Which operating system are you using? Linux
Which revision are you using? 7e665c5da059 tip
Example:
This only seems to happen when you haven't first set a precedent by encoding a []Object
(regardless if it's a different encoder). Running gob.Register on an []Object does not
have the same effect.
package main
import (
"bytes"
"encoding/gob"
"testing"
)
type Object struct {
Num int
Children []*Object
}
func TestGobPtrSlices(t *testing.T) {
var err error
b := &bytes.Buffer{}
// Works if you uncomment this
// fos := []Object{
// Object{1, nil},
// Object{2, nil},
// }
// err = gob.NewEncoder(b).Encode(&fos)
// if err != nil {
// t.Fatal("Couldn't encode:", err)
// }
// var nfos []Object
// err = gob.NewDecoder(b).Decode(&nfos)
// if err != nil {
// t.Fatal("Couldn't decode:", err)
// }
os := []*Object{
&Object{1, nil},
&Object{2, nil},
}
err = gob.NewEncoder(b).Encode(&os)
if err != nil {
t.Fatal("Couldn't encode:", err)
}
var nos []*Object
err = gob.NewDecoder(b).Decode(&nos)
if err != nil {
t.Fatal("Couldn't decode:", err)
}
for _, v := range nos {
assert(t, v)
}
}
func assert(t *testing.T, o *Object) {
if o.Num != 1 && o.Num != 2 {
t.Error("o.Num not 1 or 2, but", o.Num)
}
for _, v := range o.Children {
assert(t, v)
}
}
The text was updated successfully, but these errors were encountered:
Does look like a bug: the only difference I see between the commented-out
code and the not-working code is that the former uses []Object instead of []*Object.
There are no nil pointers being transmitted as part of a slice (but there are nil
slices).
The strange thing is that the decoding of the []*Object will actually work if you
uncomment the first block (when it has decoded/registered in some Gob global an []Object
previously.)
I figured it would be due to an []Object being gob.Register'ed first, but doing that did
not have the same effect.
The text was updated successfully, but these errors were encountered: