为什么 Equal 和 DeepEqual 之间存在差异?
s1 := "abc"
s2 := "abc"
sv1 := reflect.ValueOf(s1)
sv2 := reflect.ValueOf(s2)
fmt.Println(sv1.Equal(sv2))
fmt.Println(reflect.DeepEqual(sv1, sv2))
第一个打印 true,第二个打印 false。
正确答案
reflect.DeepEqual
似乎需要一些手动拆箱,因为它将 reflect.Value
s 视为 struct
s 而不是 reflect.Value
s ():
reflect.DeepEqual(v1.Interface(), v2.Interface())