-5

in my ambitions to understand how gob work . i have severals question .

i know that gob serialize a go type like struct map or interface(we must register it's real type) but :

func (dec *Decoder) Decode(e interface{}) error
Decode reads the next value from the input stream and stores it in the data represented by the       
empty interface value.
If e is nil, the value will be discarded. 
Otherwise, the value underlying e must be a pointer to the correct type for the next data item received.
If the input is at EOF, Decode returns io.EOF and does not modify e.

i didn't understand nothing in this documentation . what they mean by ( reads the next value from the input stream ) they are one data that we could send it's a struct or a map but not many .what they mean by If e is nil, the value will be discarded. please expert explain to me i'am disasperate all day and ididn't find nothing

4

1 回答 1

5

自从输入这个答案后,我了解到 OP 在欺骗我们。停止喂巨魔。

您可以将多个值写入流。您可以从流中读取多个值。

此代码将两个值写入输出流 w,一个 io.Writer:

e := gob.NewEncoder(w)
err := e.Encode(v1)
if err != nil {
   // handle error
}
err := e.Encode(v2)
if err != nil {
  // handle error
}

此代码从 io.Reader 流 r 中读取值。每次对 Decode 的调用都会读取一个由对 Decode 的调用写入的值。

d := gob.NewDecoder(r)
var v1 V
err := e.Decode(&v1)
if err != nil {
   // handle error
}
var v2 V
err := e.Decode(&v2)
if err != nil {
  // handle error
}

将多个值写入流会提高效率,因为有关每种编码类型的信息都会写入流一次。

于 2015-09-23T04:44:34.697 回答