DeepEqual []interface{}

1.6k Views Asked by At

Looking at the following golang code:

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")
}

Am I expecting too much of DeepEqual? Reading the documentation, the following statements reinforce my assumption that it should work:

  • Array values are deeply equal when their corresponding elements are deeply equal.
  • Interface values are deeply equal if they hold deeply equal concrete values.
  • Slice values are deeply equal when (...) or their corresponding elements (up to length) are deeply equal.

What am I missing here?

1

There are 1 best solutions below

6
Jonathan Hall On

You're comparing a []interface{} against []string, which should never be equal.

if reflect.DeepEqual(value.([]interface{}), targetValue) {

compared against targetValue which is of type []string:

var targetValue interface{} = []string{"a", "c"}