Nathan H
Nathan H

Reputation: 49371

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", "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:

What am I missing here?

Upvotes: 1

Views: 1801

Answers (1)

Jonathan Hall
Jonathan Hall

Reputation: 79576

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

Upvotes: 5

Related Questions