dongmei1988 2015-09-23 04:07
浏览 104
已采纳

试图了解Gob编码器的工作原理

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

  • 写回答

1条回答 默认 最新

  • doukao8851 2015-09-23 04:44
    关注

    Since entering this answer, I learned that OP is trolling us. Stop feeding the troll.

    You can write multiple values to a stream. You can read multiple values from a stream.

    This code writes two values to output stream w, an io.Writer:

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

    This code reads the values from stream r, an io.Reader. Each call to Decode reads a value that was written by a call to 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
    }
    

    Writing multiple values to a stream gains efficiency because information about each encoded type is written once to the stream.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line