mardi 6 mars 2018

DeepEqual []interface{}

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", "c"}
if reflect.DeepEqual(value.([]interface{}), targetValue) {
    t.Error("expected [a,b], got", result)
}

This is part of a test that I want to fail on purpose. Notice I am comparing [a b] to [a c]. Yet the test passes. It always returns true.

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?





Aucun commentaire:

Enregistrer un commentaire