dongmen5867 2016-06-22 08:02
浏览 66
已采纳

料滴尝试解码nil值会导致EOF错误

I need to use the gob to encode some data, however, I find that "type nil" can not be processed correctly (go 1.6.2)

https://play.golang.org/p/faypK8uobF

package main

import (
    "bytes"
    "encoding/gob"
    "log"
)

type T struct {
    A int
}

func init() {
    gob.Register(map[string]interface{}{})
    gob.Register(new(T))
}
func main() {
    bys := bytes.NewBuffer(nil)
    gob.NewEncoder(bys).Encode(map[string]interface{}{
        "v": (*T)(nil),
    })
    out := map[string]interface{}{}
    if err := gob.NewDecoder(bys).Decode(&out); err != nil {
        log.Panic(err)
    }
    return
}

Output:

panic: EOF
  • 写回答

1条回答 默认 最新

  • dongshilve4392 2016-06-22 10:43
    关注

    You are swallowing an error returned by Encoder.Encode():

    err := gob.NewEncoder(bys).Encode(map[string]interface{}{
        "v": (*T)(nil),
    })
    if err != nil {
        fmt.Println(err)
    }
    

    Output:

    gob: gob: cannot encode nil pointer of type *main.T inside interface
    

    This is generated by unexported method Encoder.encodeInterface(). Quoting from encode.go, unexported method Encoder.encodeInterface():

    // Gobs can encode nil interface values but not typed interface
    // values holding nil pointers, since nil pointers point to no value.
    elem := iv.Elem()
    if elem.Kind() == reflect.Ptr && elem.IsNil() {
        errorf("gob: cannot encode nil pointer of type %s inside interface", iv.Elem().Type())
    }
    

    So your Encoder.Encode() fails, it writes nothing to its output (which is the bys buffer), so attempting to read (decode) anything from it results in EOF.

    But why can't you encode an interface{} value holding a nil pointer? Quoting from the package doc of encoding/gob:

    Pointers are not transmitted, but the things they point to are transmitted; that is, the values are flattened.

    Your interface{} contains a value of pointer type, but that pointer is nil, it points to nothing, it cannot be flattened.


    Here is a related issue on github: encoding/gob: panic on encoding nil pointer #3704

    Russ:

    gob doesn't know what a pointer is: everything gets flattened. Putting a nil pointer in an interface{} value creates a non-zero value (it's not the nil interface) that gob cannot send (it can't express 'nil pointer').

    Rob Pike:

    Correct. An interface value can be transmitted only if the concrete value itself is transmittable. At least for now, that's equivalent to saying that interfaces holding typed nil pointers cannot be sent.

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

报告相同问题?

悬赏问题

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