Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / gob / doc.go
blob2e7232db51d574d0ecb18ada3912cd7766820154
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 /*
6 The gob package manages streams of gobs - binary values exchanged between an
7 Encoder (transmitter) and a Decoder (receiver). A typical use is transporting
8 arguments and results of remote procedure calls (RPCs) such as those provided by
9 package "rpc".
11 A stream of gobs is self-describing. Each data item in the stream is preceded by
12 a specification of its type, expressed in terms of a small set of predefined
13 types. Pointers are not transmitted, but the things they point to are
14 transmitted; that is, the values are flattened. Recursive types work fine, but
15 recursive values (data with cycles) are problematic. This may change.
17 To use gobs, create an Encoder and present it with a series of data items as
18 values or addresses that can be dereferenced to values. The Encoder makes sure
19 all type information is sent before it is needed. At the receive side, a
20 Decoder retrieves values from the encoded stream and unpacks them into local
21 variables.
23 The source and destination values/types need not correspond exactly. For structs,
24 fields (identified by name) that are in the source but absent from the receiving
25 variable will be ignored. Fields that are in the receiving variable but missing
26 from the transmitted type or value will be ignored in the destination. If a field
27 with the same name is present in both, their types must be compatible. Both the
28 receiver and transmitter will do all necessary indirection and dereferencing to
29 convert between gobs and actual Go values. For instance, a gob type that is
30 schematically,
32 struct { a, b int }
34 can be sent from or received into any of these Go types:
36 struct { a, b int } // the same
37 *struct { a, b int } // extra indirection of the struct
38 struct { *a, **b int } // extra indirection of the fields
39 struct { a, b int64 } // different concrete value type; see below
41 It may also be received into any of these:
43 struct { a, b int } // the same
44 struct { b, a int } // ordering doesn't matter; matching is by name
45 struct { a, b, c int } // extra field (c) ignored
46 struct { b int } // missing field (a) ignored; data will be dropped
47 struct { b, c int } // missing field (a) ignored; extra field (c) ignored.
49 Attempting to receive into these types will draw a decode error:
51 struct { a int; b uint } // change of signedness for b
52 struct { a int; b float } // change of type for b
53 struct { } // no field names in common
54 struct { c, d int } // no field names in common
56 Integers are transmitted two ways: arbitrary precision signed integers or
57 arbitrary precision unsigned integers. There is no int8, int16 etc.
58 discrimination in the gob format; there are only signed and unsigned integers. As
59 described below, the transmitter sends the value in a variable-length encoding;
60 the receiver accepts the value and stores it in the destination variable.
61 Floating-point numbers are always sent using IEEE-754 64-bit precision (see
62 below).
64 Signed integers may be received into any signed integer variable: int, int16, etc.;
65 unsigned integers may be received into any unsigned integer variable; and floating
66 point values may be received into any floating point variable. However,
67 the destination variable must be able to represent the value or the decode
68 operation will fail.
70 Structs, arrays and slices are also supported. Strings and arrays of bytes are
71 supported with a special, efficient representation (see below).
73 Interfaces, functions, and channels cannot be sent in a gob. Attempting
74 to encode a value that contains one will fail.
76 The rest of this comment documents the encoding, details that are not important
77 for most users. Details are presented bottom-up.
79 An unsigned integer is sent one of two ways. If it is less than 128, it is sent
80 as a byte with that value. Otherwise it is sent as a minimal-length big-endian
81 (high byte first) byte stream holding the value, preceded by one byte holding the
82 byte count, negated. Thus 0 is transmitted as (00), 7 is transmitted as (07) and
83 256 is transmitted as (FE 01 00).
85 A boolean is encoded within an unsigned integer: 0 for false, 1 for true.
87 A signed integer, i, is encoded within an unsigned integer, u. Within u, bits 1
88 upward contain the value; bit 0 says whether they should be complemented upon
89 receipt. The encode algorithm looks like this:
91 uint u;
92 if i < 0 {
93 u = (^i << 1) | 1 // complement i, bit 0 is 1
94 } else {
95 u = (i << 1) // do not complement i, bit 0 is 0
97 encodeUnsigned(u)
99 The low bit is therefore analogous to a sign bit, but making it the complement bit
100 instead guarantees that the largest negative integer is not a special case. For
101 example, -129=^128=(^256>>1) encodes as (FE 01 01).
103 Floating-point numbers are always sent as a representation of a float64 value.
104 That value is converted to a uint64 using math.Float64bits. The uint64 is then
105 byte-reversed and sent as a regular unsigned integer. The byte-reversal means the
106 exponent and high-precision part of the mantissa go first. Since the low bits are
107 often zero, this can save encoding bytes. For instance, 17.0 is encoded in only
108 three bytes (FE 31 40).
110 Strings and slices of bytes are sent as an unsigned count followed by that many
111 uninterpreted bytes of the value.
113 All other slices and arrays are sent as an unsigned count followed by that many
114 elements using the standard gob encoding for their type, recursively.
116 Structs are sent as a sequence of (field number, field value) pairs. The field
117 value is sent using the standard gob encoding for its type, recursively. If a
118 field has the zero value for its type, it is omitted from the transmission. The
119 field number is defined by the type of the encoded struct: the first field of the
120 encoded type is field 0, the second is field 1, etc. When encoding a value, the
121 field numbers are delta encoded for efficiency and the fields are always sent in
122 order of increasing field number; the deltas are therefore unsigned. The
123 initialization for the delta encoding sets the field number to -1, so an unsigned
124 integer field 0 with value 7 is transmitted as unsigned delta = 1, unsigned value
125 = 7 or (01 07). Finally, after all the fields have been sent a terminating mark
126 denotes the end of the struct. That mark is a delta=0 value, which has
127 representation (00).
129 Interface types are not checked for compatibility; all interface types are
130 treated, for transmission, as members of a single "interface" type, analogous to
131 int or []byte - in effect they're all treated as interface{}. Interface values
132 are transmitted as a string identifying the concrete type being sent (a name
133 that must be pre-defined by calling Register), followed by a byte count of the
134 length of the following data (so the value can be skipped if it cannot be
135 stored), followed by the usual encoding of concrete (dynamic) value stored in
136 the interface value. (A nil interface value is identified by the empty string
137 and transmits no value.) Upon receipt, the decoder verifies that the unpacked
138 concrete item satisfies the interface of the receiving variable.
140 The representation of types is described below. When a type is defined on a given
141 connection between an Encoder and Decoder, it is assigned a signed integer type
142 id. When Encoder.Encode(v) is called, it makes sure there is an id assigned for
143 the type of v and all its elements and then it sends the pair (typeid, encoded-v)
144 where typeid is the type id of the encoded type of v and encoded-v is the gob
145 encoding of the value v.
147 To define a type, the encoder chooses an unused, positive type id and sends the
148 pair (-type id, encoded-type) where encoded-type is the gob encoding of a wireType
149 description, constructed from these types:
151 type wireType struct {
152 s structType
154 type arrayType struct {
155 commonType
156 Elem typeId
157 Len int
159 type commonType {
160 name string // the name of the struct type
161 _id int // the id of the type, repeated for so it's inside the type
163 type sliceType struct {
164 commonType
165 Elem typeId
167 type structType struct {
168 commonType
169 field []*fieldType // the fields of the struct.
171 type fieldType struct {
172 name string // the name of the field.
173 id int // the type id of the field, which must be already defined
175 type mapType struct {
176 commonType
177 Key typeId
178 Elem typeId
181 If there are nested type ids, the types for all inner type ids must be defined
182 before the top-level type id is used to describe an encoded-v.
184 For simplicity in setup, the connection is defined to understand these types a
185 priori, as well as the basic gob types int, uint, etc. Their ids are:
187 bool 1
188 int 2
189 uint 3
190 float 4
191 []byte 5
192 string 6
193 complex 7
194 interface 8
195 // gap for reserved ids.
196 wireType 16
197 arrayType 17
198 commonType 18
199 sliceType 19
200 structType 20
201 fieldType 21
202 // 22 is slice of fieldType.
203 mapType 23
205 In summary, a gob stream looks like
207 ((-type id, encoding of a wireType)* (type id, encoding of a value))*
209 where * signifies zero or more repetitions and the type id of a value must
210 be predefined or be defined before the value in the stream.
212 package gob
215 For implementers and the curious, here is an encoded example. Given
216 type Point struct {x, y int}
217 and the value
218 p := Point{22, 33}
219 the bytes transmitted that encode p will be:
220 1f ff 81 03 01 01 05 50 6f 69 6e 74 01 ff 82 00
221 01 02 01 01 78 01 04 00 01 01 79 01 04 00 00 00
222 07 ff 82 01 2c 01 42 00
223 They are determined as follows.
225 Since this is the first transmission of type Point, the type descriptor
226 for Point itself must be sent before the value. This is the first type
227 we've sent on this Encoder, so it has type id 65 (0 through 64 are
228 reserved).
230 1f // This item (a type descriptor) is 31 bytes long.
231 ff 81 // The negative of the id for the type we're defining, -65.
232 // This is one byte (indicated by FF = -1) followed by
233 // ^-65<<1 | 1. The low 1 bit signals to complement the
234 // rest upon receipt.
236 // Now we send a type descriptor, which is itself a struct (wireType).
237 // The type of wireType itself is known (it's built in, as is the type of
238 // all its components), so we just need to send a *value* of type wireType
239 // that represents type "Point".
240 // Here starts the encoding of that value.
241 // Set the field number implicitly to -1; this is done at the beginning
242 // of every struct, including nested structs.
243 03 // Add 3 to field number; now 2 (wireType.structType; this is a struct).
244 // structType starts with an embedded commonType, which appears
245 // as a regular structure here too.
246 01 // add 1 to field number (now 0); start of embedded commonType.
247 01 // add 1 to field number (now 0, the name of the type)
248 05 // string is (unsigned) 5 bytes long
249 50 6f 69 6e 74 // wireType.structType.commonType.name = "Point"
250 01 // add 1 to field number (now 1, the id of the type)
251 ff 82 // wireType.structType.commonType._id = 65
252 00 // end of embedded wiretype.structType.commonType struct
253 01 // add 1 to field number (now 1, the field array in wireType.structType)
254 02 // There are two fields in the type (len(structType.field))
255 01 // Start of first field structure; add 1 to get field number 0: field[0].name
256 01 // 1 byte
257 78 // structType.field[0].name = "x"
258 01 // Add 1 to get field number 1: field[0].id
259 04 // structType.field[0].typeId is 2 (signed int).
260 00 // End of structType.field[0]; start structType.field[1]; set field number to -1.
261 01 // Add 1 to get field number 0: field[1].name
262 01 // 1 byte
263 79 // structType.field[1].name = "y"
264 01 // Add 1 to get field number 1: field[0].id
265 04 // struct.Type.field[1].typeId is 2 (signed int).
266 00 // End of structType.field[1]; end of structType.field.
267 00 // end of wireType.structType structure
268 00 // end of wireType structure
270 Now we can send the Point value. Again the field number resets to -1:
272 07 // this value is 7 bytes long
273 ff 82 // the type number, 65 (1 byte (-FF) followed by 65<<1)
274 01 // add one to field number, yielding field 0
275 2c // encoding of signed "22" (0x22 = 44 = 22<<1); Point.x = 22
276 01 // add one to field number, yielding field 1
277 42 // encoding of signed "33" (0x42 = 66 = 33<<1); Point.y = 33
278 00 // end of structure
280 The type encoding is long and fairly intricate but we send it only once.
281 If p is transmitted a second time, the type is already known so the
282 output will be just:
284 07 ff 82 01 2c 01 42 00
286 A single non-struct value at top level is transmitted like a field with
287 delta tag 0. For instance, a signed integer with value 3 presented as
288 the argument to Encode will emit:
290 03 04 00 06
292 Which represents:
294 03 // this value is 3 bytes long
295 04 // the type number, 2, represents an integer
296 00 // tag delta 0
297 06 // value 3