DeepEqual [] interface {}

问题描述 投票:0回答:1

查看以下golang代码:

b := []byte(`["a", "b"]`)
var value interface{}
json.Unmarshal(b, &value)
fmt.Println(value)                 // Print [a b]
fmt.Println(reflect.TypeOf(value)) //Print []interface {}
var targetValue interface{} = []string{"a", "b"}
if reflect.DeepEqual(value.([]interface{}), targetValue) {
    t.Error("please be equal")
}

我期待太多的DeepEqual?阅读文档,以下语句强化了我的假设它应该工作:

  • 当相应的元素非常相等时,数组值非常相等。
  • 如果它们具有非常相同的具体值,则接口值非常相等。
  • 当(...)或它们的相应元素(直到长度)非常相等时,切片值非常相等。

我在这里错过了什么?

go reflection deepequals
1个回答
3
投票

你将[]interface{}[]string进行比较,if reflect.DeepEqual(value.([]interface{}), targetValue) { 永远不会相等。

targetValue

[]string类型的var targetValue interface{} = []string{"a", "c"} 相比:

qazxswpoi
© www.soinside.com 2019 - 2024. All rights reserved.