Golang program to remove all items from hash collection


In Golang we can use delete keyword or empy map to remove all the elements from hash collection. Hashmap is a data structure from hash collection. It is an implementation of hash table which takes O(1) constant time to execute. In this program we will remove all items from hash collection using two methods.

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.

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.

Using Delete Keyword

In this illustration, we will write a Golang program to create a hashmap with desired key:value pairs. Then we will run a loop till the range of hashmap and delete each item on every iteration using delete keyword.

Algorithm

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

  • Step 2 − Create a hashmap using map literal with keys of type string and values of type int.

  • Step 3 − Assign the suitable values to the keys such that key1=10, key2=20, key3=30.

  • Step 4 − Run a loop till the range of hashmap and delete the keys inside the map by giving hashmap and key as inputs to delete function. Delete each key on every iteration.

  • Step 5 − Print the hashmap on the console using fmt.Println() function from fmt package where ln signifies new line.

Example

The following example will help us understand how to create Golang program to remove all items from hash collection using delete keyword

package main

import "fmt"

func main() {
	
   hashmap := map[string]int{
      "key1": 10,
      "key2": 20,
      "key3": 30,
   }
	
   for key := range hashmap {
      delete(hashmap, key)
   }            
   fmt.Println("The map after its elements are removed:")
   fmt.Println(hashmap) 
}

Output

The map after its elements is removed:
map[]

Using an Empty Map

In this method, we will create a hashmap similar to the last example using map literal. Then we will create another map which will be empty and assign the hashmap that empty map. The elements will be removed and the Output will be printed on the console.

Algorithm

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

  • Step 2 − Create a hashmap using map function with key of type string and values of type int.

  • Step 3 − In this step, assign the values to the key such that key1=10, key2=20, key3=30.

  • Step 4 − Now, assign hashmap an empty map which is created using make function which is a type of built-in function.

  • Step 5 − Print the hashmap on the console, basically an empty map.

  • Step 6 − The print statement is executed using fmt.Println() function where ln means new line.

Example

The following example shows how to create Golang program to remove all items from hash collection using an empty map

package main

import "fmt"

func main() {
	
   hashmap := map[string]int{
      "key1": 10,
      "key2": 20,
      "key3": 30,
   }
	
   hashmap = make(map[string]int)	
   fmt.Println("The hashmap after its elements are removed:")
   fmt.Println(hashmap) 
}

Output

The hashmap after its elements are removed:
map[]

Conclusion

We executed the program of removing all items from hash collection using two examples. In the first method we used delete keyword to remove the items and in the second example we used an empty map to execute the program. Both the examples returned desired Output. Hence, program executed successfully.

Updated on: 03-Apr-2023

172 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements