How to compare struct, slice, map are equal?

2019-01-08 09:54发布

I want to check that two structs are equal, but have some problem:

package main

import (
"fmt"
"reflect"
)

type T struct {
    X int
    Y string
    Z []int
    M map[string]int
}

func main() {
    t1 := T{
        X:1,
        Y:"lei",
        Z:[]int{1,2,3},
        M:map[string]int{
            "a":1,
            "b":2,
        },
    }

    t2 :=  T{
        X:1,
        Y:"lei",
        Z:[]int{1,2,3},
        M:map[string]int{
            "a":1,
            "b":2,
        },
    }


    fmt.Println(t2 == t1)
    //error - invalid operation: t2 == t1 (struct containing []int cannot be compared)


    fmt.Println(reflect.ValueOf(t2) == reflect.ValueOf(t1))
    //false
    fmt.Println(reflect.TypeOf(t2) == reflect.TypeOf(t1))
    //true


    //Update: slice or map
    a1 := []int{1,2,3,4}
    a2 := []int{1,2,3,4}

    fmt.Println(a1==a2)
    //invalid operation: a1 == a2 (slice can only be compared to nil)

    m1 := map[string]int{   
        "a":1,
        "b":2,
    }
    m2 := map[string]int{   
        "a":1,
        "b":2,
    }
    fmt.Println(m1==m2)
    // m1 == m2 (map can only be compared to nil)
}

http://play.golang.org/p/AZIzW2WunI

标签: go go-reflect
4条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-08 10:15

reflect.DeepEqual is often incorrectly used to compare two like structs, as in your question.

cmp.Equal is a better tool for comparing structs.

To see why reflection is ill-advised, let's look at the documentation:

Struct values are deeply equal if their corresponding fields, both exported and unexported, are deeply equal.

....

numbers, bools, strings, and channels - are deeply equal if they are equal using Go's == operator.

If we compare two time.Time values of the same UTC time, t1 == t2 will be false if they're metadata timezone is different.

go-cmp looks for the Equal() method and uses that to correctly compare times.

Example:

m1 := map[string]int{
    "a": 1,
    "b": 2,
}
m2 := map[string]int{
    "a": 1,
    "b": 2,
}
fmt.Println(cmp.Equal(m1, m2)) // will result in true
查看更多
不美不萌又怎样
3楼-- · 2019-01-08 10:21

The reflect.DeepEqual can help you. You should note the situation with nil. You can implement an ObjectEqual function like this:

func ObjectEqual(a, b interface{}) bool {
    if a == nil || b == nil {
        return a == b
    }
    return reflect.DeepEqual(a, b)
}
查看更多
不美不萌又怎样
4楼-- · 2019-01-08 10:30

Here's how you'd roll your own function http://play.golang.org/p/Qgw7XuLNhb

func compare(a, b T) bool {
  if &a == &b {
    return true
  }
  if a.X != b.X || a.Y != b.Y {
    return false
  }
  if len(a.Z) != len(b.Z) || len(a.M) != len(b.M) {
    return false
  }
  for i, v := range a.Z {
    if b.Z[i] != v {
      return false
    }
  }
  for k, v := range a.M {
    if b.M[k] != v {
      return false
    }
  }
  return true
}
查看更多
beautiful°
5楼-- · 2019-01-08 10:34

You can use reflect.DeepEqual, or you can implement your own function (which performance wise would be better than using reflection):

http://play.golang.org/p/CPdfsYGNy_

m1 := map[string]int{   
    "a":1,
    "b":2,
}
m2 := map[string]int{   
    "a":1,
    "b":2,
}
fmt.Println(reflect.DeepEqual(m1, m2))
查看更多
登录 后发表回答