Golang program to merge two hash collections


In golang we can merge two hash collection by using map function. A hashmap is present in hash collections. It stores key:value pairs. We can perform various operations on a hashmap like merging them. In this article we will merge two hashmaps using a method. In that method, we will create an additional map to store the merged key:value pairs. Then, the map will be printed on the terminal using fmt package.

Algorithm

  • Create a package main and declare fmt(format package) in the program where main produces executable codes and fmt helps in formatting input and output.

  • In the main function create a hashmap1 using map literal with keys and values both of type string.

  • Create another hashmap2 similarly like we did in step2 but here assign the item2 a new value.

  • Create another map named merge_hashmap using make function which is a built-in function and map literal in Golang.

  • Iterate the hashmap1 and in every iteration add the value to the corresponding key.

  • Similarly, Iterate the hashmap2 and add the corresponding values to the keys.

  • After the key:value pairs are merged in the map, print the map on the console using fmt package’s Println function where ln means new line.

Syntax

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments.

Example

In this example, Firstly, we will create two hashmaps whose key:value pairs are to be merged. Then, another map will be created to add the values in it and finally the output will be printed on the console. Let’s have a close look at the code and the algorithm.

//Golang program to merge two hash collections
package main

import "fmt" //import fmt package in the program

//Main function to execute the program
func main() {
   hashmap1 := map[string]string{  //create hashmap1 using map literal
      "item1": "value1",
      "item2": "value2",
   }
   hashmap2 := map[string]string{   //create hashmap2 using map literal
      "item2": "new_value",
      "item3": "value3",
   }
   
   merge_hashmap := make(map[string]string)  //create this map to store the merged values
   
   for key, value := range hashmap1 {
      merge_hashmap[key] = value //iterate the map1 to add values in new map
   }
   
   for key, value := range hashmap2 {
      merge_hashmap[key] = value //iterate the map2 to add values in new map 
   }
   
   fmt.Println("The map after its merged:")
   fmt.Println("Merged hash:", merge_hashmap)//print the new map 
}

Output

The map after its merged:
Merged hash: map[item1:value1 item2:new_value item3:value3]

Conclusion

We compiled and executed the program of merging two hash collections using a simple example. In this example described above we created an additional map to store and merge the key:value pairs of the two hashmaps.

Updated on: 27-Mar-2023

240 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements