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