In Go, a map is a powerful and versatile data structure that consists of unordered key-value pairs. It offers fast lookups and allows for efficient updates and deletions based on keys. To compare two maps in Go, you can use the DeepEqual() function provided by the reflect package. This function checks if both maps are deeply equal based on certain conditions.
Example
package main
import (
"fmt"
"reflect"
)
func main() {
// Define maps
map1 := map[string]int{
"a": 5,
"b": 3,
"c": 4,
}
map2 := map[string]int{
"a": 5,
"b": 3,
"c": 4,
}
// Using DeepEqual function
result := reflect.DeepEqual(map1, map2)
fmt.Println("Are map1 and map2 equal? :", result)
}
Syntax
reflect.DeepEqual(a, b)
Table of Content
Comparing Maps
In Go language, you are allowed to compare two maps with each other using DeepEqual() function provided by the reflect package as shown in the above example.
Syntax
reflect.DeepEqual(a, b)
Comparison Scenarios
Now, letâs explore different scenarios for comparing maps using the same basic example.
Comparing Two Identical Maps
In this case, we are comparing map1 and map2, which are identical.
package main
import (
"fmt"
"reflect"
)
func main() {
// Define maps
map1 := map[string]int{
"a": 5,
"b": 3,
"c": 4,
}
map2 := map[string]int{
"a": 5,
"b": 3,
"c": 4,
}
// Using DeepEqual function
result := reflect.DeepEqual(map1, map2)
fmt.Println("Are map1 and map2 equal? :", result)
}
Output
Are map1 and map2 equal? : true
Comparing a Map with an Extra Entry
Letâs create a third map with an additional entry to see how the comparison behaves.
Example
package main
import (
"fmt"
"reflect"
)
func main() {
// Define maps
map1 := map[string]int{
"a": 5,
"b": 3,
"c": 4,
}
map2 := map[string]int{
"a": 5,
"b": 3,
"c": 4,
}
map3 := map[string]int{
"a": 5,
"b": 3,
"c": 4,
"d": 2, // Extra entry
}
// Using DeepEqual function
result := reflect.DeepEqual(map1, map3)
fmt.Println("Are map1 and map3 equal? :", result)
// Optionally, you can compare map1 with map2
result = reflect.DeepEqual(map1, map2)
fmt.Println("Are map1 and map2 equal? :", result)
}
Output
Are map1 and map3 equal? : false Are map1 and map2 equal? : true
Comparing Maps with Different Key-Value Pairs
Here, we will compare map1 with a map that has different key-value pairs.
Example
package main
import (
"fmt"
"reflect"
)
func main() {
// Define maps
map1 := map[string]int{
"a": 5,
"b": 3,
"c": 4,
}
map4 := map[string]int{
"a": 5,
"b": 2, // Different value
"c": 4,
}
// Using DeepEqual function
result := reflect.DeepEqual(map1, map4)
fmt.Println("Are map1 and map4 equal? :", result)
}
Output
Are map1 and map4 equal? : false
Comparing Maps with Different Keys
In this scenario, we will compare map1 with a map that has different keys.
Example
package main
import (
"fmt"
"reflect"
)
func main() {
// Define maps
map1 := map[string]int{
"a": 5,
"b": 3,
"c": 4,
}
map5 := map[string]int{
"e": 5,
"f": 3, // Different Keys
"g": 4,
}
// Using DeepEqual function
result := reflect.DeepEqual(map1, map5)
fmt.Println("Are map1 and map5 equal? :", result)
}
Output
Are map1 and map5 equal? : false