gob编码有什么作用?
What does gob encoding do?
gob encoding/decoding 有什么作用吗?在下面的示例中,数据在解码前后看起来是一样的。我很困惑,请指教
data = "ABC"
buf := new(bytes.Buffer)
//glob encoding
enc := gob.NewEncoder(buf)
enc.Encode(data)
fmt.Println("Encoded:", data) //Encoded: ABC
//glob decoding
d := gob.NewDecoder(buf)
d.Decode(data)
fmt.Println("Decoded: ", data) //Decoded: ABC
您的比较是错误的 - 将正在编码的数据 (data
) 与解码后的结果 (d.Decode(data)
) 进行比较,显然会导致相同的结果(如果一切正常预期的)。
编码本身将显示在下划线字节缓冲区中(尝试打印缓冲区本身 - fmt.Println(buf.Bytes())
)。
阅读更多关于the gob package
gob encoding/decoding 有什么作用吗?在下面的示例中,数据在解码前后看起来是一样的。我很困惑,请指教
data = "ABC"
buf := new(bytes.Buffer)
//glob encoding
enc := gob.NewEncoder(buf)
enc.Encode(data)
fmt.Println("Encoded:", data) //Encoded: ABC
//glob decoding
d := gob.NewDecoder(buf)
d.Decode(data)
fmt.Println("Decoded: ", data) //Decoded: ABC
您的比较是错误的 - 将正在编码的数据 (data
) 与解码后的结果 (d.Decode(data)
) 进行比较,显然会导致相同的结果(如果一切正常预期的)。
编码本身将显示在下划线字节缓冲区中(尝试打印缓冲区本身 - fmt.Println(buf.Bytes())
)。
阅读更多关于the gob package