
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Replace Items of Hash Collection from Another Hash Collection in Go
In this Go language article, we learn how to replace the items of hash collection from another hash collection using ok idiom method as well as a loop to replace the items of hash collection. A hash collection contains a hashmap which is an efficient data structure
Syntax
func range(variable)
The range function is used to iterate over any data type. To use this, we first have to write the range keyword followed by the data type to which we want to iterate and as a result the loop will iterate till the last element of the variable.
Algorithm
Step 1 ? This program imports two packages fmt and main where fmt helps in the formatting of input and output and main helps in generating executable codes
Step 2 ? Create a main function
Step 3 ? In the main, create a map with keys of type string and values of type int
Step 4 ? Set the keys and values in the map
Step 5 ? Create another replacement map with keys and values both of type string
Step 6 ? Iterate the replacement map and in that map, add the values from the original map to it using ok idiom
Step 7 ? Print the original map on the console
Step 8 ? The print statement is executed using Println function from the fmt package where ln means new line
Example 1
In this Example, we will create an original hashmap and a replacement map using map literal with both keys and the values of type string. We will replace the data in the original map using ok idiom in the for loop while iterating the replacement map.
package main import "fmt" func main() { original_hashmap := map[string]string{ "item1": "Pizza", "item2": "Pasta", "item3": "Maggi", } replacement_hashmap := map[string]string{ "item2": "Noodles", "item3": "Burger", "item4": "momos", } for key, value := range replacement_hashmap { if _, ok := original_hashmap[key]; ok { original_hashmap[key] = value } } fmt.Println("The updated hash collection is:") fmt.Println(original_hashmap) }
Output
The updated hash collection is: map[item1:Pizza item2:Noodles item3:Burger]
Example 2
In this Example, we will create two maps one is the original map and the replacement map. Iterate through the replacement map and add the items in the map using for loop.
package main import "fmt" func main() { original_hashmap := map[string]string{ "item1": "pizza", "item2": "pasta", "item3": "maggi", } replacement_hashmap := map[string]string{ "item2": "momos", "item3": "noodles", "item4": "burger", } for key, value := range replacement_hashmap { original_hashmap[key] = value } fmt.Println("The updated hash collection is:") fmt.Println(original_hashmap) }
Output
The updated hash collection is: map[item1:pizza item2:momos item3:noodles item4:burger]
Conclusion
We executed the program of replacing the items of hash collection from another hash collection. In the first Example, we used ok idiom along with a for loop and in the second Example we updated the original map using a for loop.