Is / gob encoding deterministic?

Can we expect for two Go x, y objects such that x equals y (without assuming any trick with interfaces and maps, just structures and arrays) that the output of gob_encode (x) and gob_encode (y) will always be the same?

+3
source share
1 answer

You do not have to worry while he is "doing his job." But the current encoding/gobimplementation is deterministic. But (keep reading)!

WITH

The flow of throats is self-describing. Each data item in the stream is preceded by a specification of its type, expressed in terms of a small set of predefined types.

, , , . , , . , , , , ref (, ) .

. :

type Int struct{ X int }

b := &bytes.Buffer{}
e := gob.NewEncoder(b)

e.Encode(Int{1})
fmt.Println(b.Bytes())

e.Encode(Int{1})
fmt.Println(b.Bytes())

e.Encode(Int{1})
fmt.Println(b.Bytes())

( ):

[23 255 129 3 1 1 3 73 110 116 1 255 130 0 1 1 1 1 88 1 4 0 0 0 5 255 130 1 2 0]
[23 255 129 3 1 1 3 73 110 116 1 255 130 0 1 1 1 1 88 1 4 0 0 0 5 255 130 1 2 0 5 255 130 1 2 0]
[23 255 129 3 1 1 3 73 110 116 1 255 130 0 1 1 1 1 88 1 4 0 0 0 5 255 130 1 2 0 5 255 130 1 2 0 5 255 130 1 2 0]

, Encode() Int [5 255 130 1 2 0], [5 255 130 1 2 0].

2 gob.Encoder, , .

, " ". , , / , , , / /id.

, gob . ( , - ), , . Go ( ).

+5

Source: https://habr.com/ru/post/1663926/


All Articles