DeepEqual
The reflect.DeepEqual()
requires a mention if we are talking about the reflect
package.
Basic data types in Go can be compared using the ==
or !=
operator, but slices and maps are not comparable using this method.
The reflect.DeepEqual()
function can be used in scenarios when the types are incomparable. For example, it can be used for comparing slices and maps. Here is an example that compares maps and slices using DeepEqual
:
package main import ( Â Â "fmt" Â Â "reflect" ) func main() { Â Â runDeepEqual(nil, nil) Â Â runDeepEqual(make([]int, 10), make([]int, 10)) Â Â runDeepEqual([3]int{1, 2, 3}, [3]int{1, 2, 3}) Â Â runDeepEqual(map[int]string{1: "one", 2: "two"}, map[int]string{2: "two", 1: "one"}) } func runDeepEqual(a, b interface{}) { Â Â fmt.Printf("%v DeepEqual %v : %v\n", a, b, reflect.DeepEqual(a, b)) }
In the preceding example...