@@ -12,14 +12,13 @@ encoding the structures of the go/ast package, which is its sole purpose.
12
12
13
13
# Encoding Scheme
14
14
15
- Every encoded value begins with a single byte that describes what (if
16
- anything) follows. There is enough information to skip over the value, since
17
- the decoder must be able to do that if it encounters a struct field it
18
- doesn't know.
15
+ Every encoded value begins with a single byte that describes what (if anything) follows.
16
+ A value's encoding contains enough information to skip over the value, since the
17
+ decoder must be able to do that if it encounters a struct field it doesn't know.
19
18
20
19
Most of the values of that initial byte can be devoted to small unsigned
21
20
integers. For example, the number 17 is represented by the single byte 17.
22
- Only a few byte values have special meaning .
21
+ Only a few byte values have special meanings, whose descriptions follow .
23
22
24
23
The nil code indicates that the value is nil. We don't absolutely need this:
25
24
we could always represent the nil value for a type as something that couldn't
@@ -37,10 +36,16 @@ example, the string "hi" is represented as:
37
36
38
37
Unsigned integers that can't fit into the initial byte are encoded as byte
39
38
sequences of length 4 or 8, holding little-endian uint32 or uint64 values. We
40
- use uint32s where possible to save space. We could have saved more space by
41
- also considering 16-byte numbers, or using a variable-length encoding like
42
- varints or gob's representation, but it didn't seem worth the additional
43
- complexity.
39
+ use uint32s where possible to save space. For example, 255 is encoded as
40
+
41
+ nBytes 4 0 0 0 255
42
+
43
+ This representation is not as space-efficient as others, but improving its space
44
+ usage didn't seem worth the additional complexity.
45
+
46
+ Signed integers use gob's encoding.
47
+
48
+ Floats are encoded by converting them to uints using math.Float64bits.
44
49
45
50
The nValues code is for sequences of values whose size is known beforehand,
46
51
like a Go slice or array. The slice []string{"hi", "bye"} is encoded as
@@ -51,6 +56,29 @@ The ref code is used to refer to an earlier encoded value. It is followed by
51
56
a uint denoting the index data of the value to use.
52
57
53
58
The start and end codes delimit a value whose length is unknown beforehand.
54
- It is used for structs.
59
+ They are used for structs.
60
+
61
+ A struct is encoded as a sequence of fields between a start code and an end code.
62
+ Each exported field is encoded as an integer field number followed by the field's value.
63
+ Fields with zero values are omitted.
64
+
65
+ The field number is initially the position of the field in the struct declaration. This
66
+ initial ordering is preserved even if fields are added or removed; new fields are
67
+ numbered after the initial ones. This is accomplished by preserving the field ordering
68
+ in a comment in the generated code. For example, say a struct S has fields A and B.
69
+ The generated encoder would assign 0 to A and 1 to B, and include this comment in the code:
70
+
71
+ // Fields of S: A B
72
+
73
+ If later a field X was added between A and B, the generator would read the comment
74
+ and preserve the original assignments of A and B. It would assign 2 to X, and the
75
+ generated code would contain the new comment
76
+
77
+ // Fields of S: A B X
78
+
79
+ Values of type any are encoded as a pair of an assigned type number and the value.
80
+ All types that can appear as values must be registered by calling Register.
81
+ Type numbers are assigned to type names by the encoder in the order that types are encountered.
82
+ The assignments are saved at the beginning of the encoded data.
55
83
*/
56
84
package codec
0 commit comments