Search code examples
gogob

Why decode two different objects into a same object, but the bool member has not been changed?


I'm using go's encoding/gob to decode two different objects which type are T into a same object, but the object's bool member has not been changed after the second decode. Why?

package main

import (
    "fmt"
    "encoding/gob"
    "bytes"
)

type T struct {
    X int
    Y string
    Z bool
}

func main() {
    t := T{}
    buf := new(bytes.Buffer)
    enc := gob.NewEncoder(buf)
    dec := gob.NewDecoder(buf)

    t1 := T{1, "1", true}
    enc.Encode(t1)
    dec.Decode(&t)
    fmt.Printf("%+v\n", t)

    // If t is a new entity, the second decode into t can product expected result: {X:2 Y:2 Z:false}
    // otherwise, t's bool member has not been changed after the second decode.
    // t = T{}
    t2 := T{2, "2", false}
    enc.Encode(t2)
    dec.Decode(&t)
    fmt.Printf("%+v\n", t)

    // result:
    // {X:1 Y:1 Z:true}
    // {X:2 Y:2 Z:true}
}

Solution

  • Base on the document: https://golang.org/pkg/encoding/gob/#hdr-Encoding_Details

    If a field has the zero value for its type (except for arrays; see above), it is omitted from the transmission.

    And "false" is zero value. If you try to set t2.X = 0 it will show you the same behavior.