How to compare if two structs, slices or maps are equal?

后端 未结 5 2300
醉话见心
醉话见心 2020-11-28 02:30

I want to check if two structs, slices and maps are equal.

But I\'m running into problems with the following code. See my comments at the relevant lines.

<         


        
5条回答
  •  误落风尘
    2020-11-28 03:07

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

提交回复
热议问题