donglu1913 2018-06-06 15:32
浏览 31
已采纳

将地图序列化为gob后,DeepEqual不正确

I've encountered some strange behavior with reflect.DeepEqual. I have an object of type map[string][]string, with one key whose value is an empty slice. When I use gob to encode this object, and then decode it into another map, these two maps are not equal according to reflect.DeepEqual (even though the content is identical).

package main

import (
    "fmt"
    "bytes"
    "encoding/gob"
    "reflect"
)

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

    // Encode m0 to bytes
    var network bytes.Buffer
    enc := gob.NewEncoder(&network)
    enc.Encode(m0)

    // Decode bytes into a new map m2
    dec := gob.NewDecoder(&network)
    m2 := make(map[string][]string)
    dec.Decode(&m2)

    fmt.Printf("%t
", reflect.DeepEqual(m0, m2)) // false
    fmt.Printf("m0: %+v != m2: %+v
", m0, m2) // they look equal to me!
}

Output:

false
m0: map[apple:[]] != m2: map[apple:[]]

A couple notes from follow-up experiments:

If I make the value of m0["apple"] a nonempty slice, for example m0["apple"] = []string{"pear"}, then DeepEqual returns true.

If I keep the value as an empty slice but I construct the identical map from scratch rather than with gob, then DeepEqual returns true:

m1 := make(map[string][]string)
m1["apple"] = []string{}
fmt.Printf("%t
", reflect.DeepEqual(m0, m1)) // true!

So it's not strictly an issue with how DeepEqual handles empty slices; it's some strange interaction between that and gob's serialization.

  • 写回答

1条回答 默认 最新

  • dongliang9576 2018-06-06 15:42
    关注

    This is because you encode an empty slice, and during decoding the encoding/gob package only allocates a slice if the one provided (the target to decode into) is not big enough to accomodate the encoded values. This is documented at: gob: Types and Values:

    In general, if allocation is required, the decoder will allocate memory. If not, it will update the destination variables with values read from the stream.

    Since there are 0 elements encoded, and a nil slice is perfectly capable of accomodating 0 elements, no slice will be allocated. We can verify this if we print the result of comparing the slices to nil:

    fmt.Println(m0["apple"] == nil, m2["apple"] == nil)
    

    Output of the above is (try it on the Go Playground):

    true false
    

    Note that the fmt package prints nil slice values and empty slices the same way: as [], you cannot rely on its output to judge if a slices is nil or not.

    And reflect.DeepEqual() treats a nil slice and an empty but non-nil slice different (non-deep equal):

    Note that a non-nil empty slice and a nil slice (for example, []byte{} and []byte(nil)) are not deeply equal.

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

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测