Nat*_*n H 1 reflection go deepequals
看下面的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")
}
Run Code Online (Sandbox Code Playgroud)
是我期待太多了吗DeepEqual?阅读文档后,以下陈述强化了我的假设:它应该有效:
我在这里缺少什么?
您正在比较 a[]interface{}与[]string,它永远不应该相等。
if reflect.DeepEqual(value.([]interface{}), targetValue) {
Run Code Online (Sandbox Code Playgroud)
targetValue与类型比较[]string:
var targetValue interface{} = []string{"a", "c"}
Run Code Online (Sandbox Code Playgroud)