2

我正在使用 go 的 encoding/gob 将类型为 T 的两个不同对象解码为同一个对象,但是在第二次解码后对象的 bool 成员没有改变。为什么?

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}
}
4

2 回答 2

2

基于文档:https ://golang.org/pkg/encoding/gob/#hdr-Encoding_Details

如果字段的类型为零(数组除外;见上文),则从传输中省略。

而“假”是零值。如果您尝试设置t2.X = 0,它将向您显示相同的行为。

于 2019-05-21T05:31:17.257 回答
0

意外行为来自重用内存而不清理它。您重复使用了两次 t 和 b,这使您面临许多可能的错误。这里是 t 产生你的问题,但它也可能是 b 。

正如 nvcnvn 所回答的,gob 编码的预期行为是不考虑类型中具有 0 值的字段。查看编码结构的字节数增加大小: https: //play.golang.org/p/HCz8-2kXHQX

如果您想重用 bytes.Buffer 而不需要任何额外的分配并且安全,请重置它的值:https ://golang.org/pkg/bytes/#Buffer.Reset

于 2019-05-22T18:45:50.467 回答