dsjz1119 2018-06-06 16:18
浏览 59
已采纳

Gob无法使用nil指针值编码地图

Gob's Encode returns an error when I try to encode a map to pointers if one of the values is nil. This seems to contradict the docs (but I may be misinterpreting the meaning):

In slices and arrays, as well as maps, all elements, even zero-valued elements, are transmitted, even if all the elements are zero.

Code:

package main

import (
    "bytes"
    "encoding/gob"
)

type Dog struct {
    Name string
}

func main() {
    m0 := make(map[string]*Dog)
    m0["apple"] = nil

    // Encode m0 to bytes
    var network bytes.Buffer
    enc := gob.NewEncoder(&network)
    err := enc.Encode(m0)
    if err != nil {
        panic(err) // Output: panic: gob: encodeReflectValue: nil element
    }
}

Output:

panic: gob: encodeReflectValue: nil element

Is there a good reason for gob to fail in this case? It seems that either of the two obvious options are preferable to failure: 1) don't encode any keys with values that are zero-valued or 2) encode all keys even if the values are zero-valued. With the current state of things, is the best practice to recursively scan through my struct to see if there are any nil map values, and delete those keys if so? If this check is required, it seems like it should be the responsibility of the encoding/gob package, not the user.

Further, the rule isn't simply "gob can't encode maps where a key has a value of nil" because if the value type is a slice, then nil is accepted:

func main() {
    m0 := make(map[string][]string)
    m0["apple"] = nil

    // Encode m0 to bytes
    var network bytes.Buffer
    enc := gob.NewEncoder(&network)
    err := enc.Encode(m0) // err is nil
    if err != nil {
        panic(err) // doesn't panic
    }
}
  • 写回答

1条回答 默认 最新

  • dqxz96998 2018-06-06 16:39
    关注

    The gob encoded stream has no notion of pointers. If you encode a pointer value such as *int, the pointed value will be sent, that is, a value of type int. This transformation is reversed at the decoder side if needed, e.g. if an int value is found in the stream for which an *int value is to be set, a pointer (of type *int) will be set pointing to the decoded int value.

    So if a pointer value itself is nil, there is no value that the gob package could encode instead of the pointer value, a nil pointer points to nothing. Dereferencing a nil pointer is a runtime panic.

    This is also documented at gob: Basics:

    Pointers are not transmitted, but the things they point to are transmitted; that is, the values are flattened. Nil pointers are not permitted, as they have no value.

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?