Comparing Maps in Golang


Maps in Golang are unordered collections of key-value pairs. They are widely used in Golang programming due to their flexibility and ease of use. In many cases, it may be necessary to compare maps to check if they are equal or not. In this article, we will explore the different methods of comparing maps in Golang.

Method 1: Looping Through the Maps

One of the simplest methods to compare two maps is by looping through them and comparing each key-value pair. Here is an example −

Example

package main

import "fmt"

func CompareMaps(map1, map2 map[string]int) bool {
   if len(map1) != len(map2) {
      return false
   }

   for key, value := range map1 {
      if val, ok := map2[key]; !ok || val != value {
         return false
      }
   }
   return true
}

func main() {
   // create two maps with same key-value pairs
   map1 := map[string]int{"one": 1, "two": 2, "three": 3}
   map2 := map[string]int{"one": 1, "two": 2, "three": 3}

   // compare the two maps
   if CompareMaps(map1, map2) {
      fmt.Println("Maps are equal")
   } else {
      fmt.Println("Maps are not equal")
   }
}

Output

Maps are equal

In the above code, we first check if the length of both maps is equal. If not, then we return false. Next, we loop through the first map and check if the corresponding key-value pair exists in the second map. If not, or if the values are not equal, we return false. If we reach the end of the loop without returning false, then the two maps are equal.

Method 2: Using reflect.DeepEqual

Another method to compare maps in Golang is to use the reflect.DeepEqual function. This function compares two variables of any type and returns true if they are deeply equal. Here is an example −

Example

package main

import (
   "fmt"
)

func main() {
   map1 := map[string]int{"a": 1, "b": 2}
   map2 := map[string]int{"a": 1, "b": 2}

   if CompareMaps(map1, map2) {
      fmt.Println("Maps are equal")
   } else {
      fmt.Println("Maps are not equal")
   }
}
func CompareMaps(map1, map2 map[string]int) bool {
   if len(map1) != len(map2) {
      return false
   }
   for key, value := range map1 {
      if val, ok := map2[key]; !ok || val != value {
         return false
      }
   }
   return true
}

Output

Maps are equal

In the above code, we import the reflect package and use the DeepEqual function to compare the two maps. If the two maps are deeply equal, then the function returns true, otherwise false.

Note that the DeepEqual function may not be the most efficient method for comparing maps, especially for large maps, as it performs a recursive comparison of all the fields.

Method 3: Marshaling Maps to JSON

A third method to compare maps in Golang is to marshal them into JSON strings and compare the resulting strings. Here is an example −

Example

package main

import (
   "encoding/json"
   "fmt"
)

func main() {
   // Define two maps with the same key-value pairs
   map1 := map[string]int{"a": 1, "b": 2, "c": 3}
   map2 := map[string]int{"a": 1, "b": 2, "c": 3}

   // Compare the two maps
   if CompareMaps(map1, map2) {
      fmt.Println("The maps are equal.")
   } else {
      fmt.Println("The maps are not equal.")
   }
}
func CompareMaps(map1, map2 map[string]int) bool {
   map1Json, _ := json.Marshal(map1)
   map2Json, _ := json.Marshal(map2)
   return string(map1Json) == string(map2Json)
}

Output

The maps are equal.

In the above code, we first marshal both maps into JSON strings using the json.Marshal function. We then compare the two resulting strings. If they are equal, then the two maps are equal.

Note that this method is not the most efficient method for comparing maps, especially for large maps, as it involves marshaling and unmarshaling the maps.

Conclusion

In this article, we explored different methods of comparing maps in Golang. We saw how to loop through maps, use the reflect.DeepEqual function, and marshal maps into JSON strings. Each method has its own advantages and disadvantages, and the choice of method depends on the specific requirements of the application. By understanding these methods, Golang developers can choose the best approach for comparing maps in their applications.

Updated on: 07-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements