小编llx*_*llx的帖子

尝试解码nil值的gob导致EOF错误

我需要使用它gob来编码一些数据,但是,我发现"type nil"无法正确处理(转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
}
Run Code Online (Sandbox Code Playgroud)

输出:

panic: EOF
Run Code Online (Sandbox Code Playgroud)

null pointers go gob

1
推荐指数
1
解决办法
1356
查看次数

标签 统计

go ×1

gob ×1

null ×1

pointers ×1