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
http://play.golang.org/p/cNlOycuLzl is this:
package main
import (
"bytes"
"encoding/gob"
"time"
)
type Update struct {
Time time.Time // *** Note1 : Works if change from time.Time -> int ***//
}
type Time struct {
T *time.Time
}
type PC struct {
Id int64
Update []Update // *** Note2 : Works if comment out one of Update or LastUpdate ***//
LastUpdate Time
}
func main() {
buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
var err error
if err = enc.Encode(PC{}); err != nil {
println("A", err.Error())
return
}
dec := gob.NewDecoder(bytes.NewBuffer(buf.Bytes()))
if err = dec.Decode(new(PC)); err != nil {
println("B", err.Error())
return
}
}
It prints "B extra data in buffer. Removing either of the time.Time values from the
struct or replacing them with ints clears up the error. Something is awry with gots and
time.
The text was updated successfully, but these errors were encountered:
It doesn't seem to be related to time. It seems to be related to two
GobEncoder/GobDecoders in the same value where one is a pointer and the other isn't:
package main
import (
"bytes"
"encoding/gob"
)
type T struct{}
func (T) GobEncode() ([]byte, error) {
return []byte{0xDE, 0xAE, 0xAD, 0xBE, 0xEF}, nil
}
func (T) GobDecode([]byte) error {
return nil
}
type PC struct {
A T
B *T
}
func main() {
buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
var err error
if err = enc.Encode(PC{}); err != nil {
println("A", err.Error())
return
}
dec := gob.NewDecoder(bytes.NewBuffer(buf.Bytes()))
if err = dec.Decode(new(PC)); err != nil {
println("B", err.Error())
return
}
}
http://play.golang.org/p/7MfiN4ToAZ
It looks like the arrangement of pointer/non-pointer fields makes a difference. If the
pointer field is first, there is no problem, in other positions the bug appears.
Dan is correct. It appears to happen because GobEncoders are treated specially and as
such, the *T is encoded immediately, despite the T having already been encoded. The
"extra data in buffer" error is actually masking the "gob: duplicate type received"
error (which we might also want to address).
I don't know if this is the proper fix, but it makes my test pass:
https://golang.org/cl/7085051
The text was updated successfully, but these errors were encountered: