PROPERLY delete an element from map in GO [SOLVED]

Different Methods in Golang to delete from map

We will discuss various techniques to delete an element from a given map in this tutorial.

In other languages it is called a dictionary for python, associative array in Php , hash tables in Java and Hash maps in JavaScript. In all these languages maps share some implementation such as delete, add and lookup of data.

The built-in delete() method of Go maps is used to remove a key-value pair from the map. The first argument to the delete function is the map from which you wish to remove elements, and the second argument is the key you want to remove as the syntax below:

Advertisement
delete(map,key)

Let's use the above function in Go to learn how it operates.

 

Example 1: Use delete() function without checking key exist or not

In this example, we will try to delete a student name from a map of students' score.

package main

import (
   "fmt"
)

func main() {
   studentsScore := make(map[string]int)
   studentsScore["Anna"] = 5
   studentsScore["Bob"] = 9
   studentsScore["Clair"] = 8
   studentsScore["Daniel"] = 10

   fmt.Println(studentsScore)

   fmt.Println("Deleting Bob from the students score map")
   delete(studentsScore, "Bob")

   fmt.Println("Deleting Eve from the students score map")
   delete(studentsScore, "Eve")

   fmt.Println(studentsScore)
}

Output:

Deleting Bob from the students score map
Deleting Eve from the students score map
map[Anna:5 Clair:8 Daniel:10]

Explanation:

  • We create studentsScore map which contains some students' name as keys and integer score values are the value .
  • Use the delete() function to get rid of the key named "Bob" and "Eve" from the map and then we again print the contents of the map.

 

Example 2: Check if element exists in map and delete

We can see that, in example 1, even "Eve" key is not in the map, the programs will run normally. But if we want to make sure we don't write a code that creates panic or handle if key not in the map, we can make use of the code shown below:

package main

import (
   "fmt"
)

func main() {
   studentsScore := make(map[string]int)
   studentsScore["Anna"] = 5
   studentsScore["Bob"] = 9
   studentsScore["Clair"] = 8
   studentsScore["Daniel"] = 10

   fmt.Println(studentsScore)

   fmt.Println("Deleting Bob from the students score map")
   deleteKey(studentsScore, "Bob")

   fmt.Println("Deleting Eve from the students score map")
   deleteKey(studentsScore, "Eve")

   fmt.Println(studentsScore)
}

func deleteKey(m map[string]int, key string) {
	if _, ok := m[key]; ok {
		delete(m, key)
	 } else {
		fmt.Println(key, "is not in the map")
	 }
}

Output:

Advertisement
map[Anna:5 Bob:9 Clair:8 Daniel:10]
Deleting Bob from the students score map
Deleting Eve from the students score map
Eve is not map key
map[Anna:5 Clair:8 Daniel:10]

The above method is advised over the previous one since it is more fail-safe.

 

Summary

In this article, I provided 2 examples of how to remove a key from a map using the delete() function. For more information about map in Golang, please visit our previous article Golang Maps Explained in layman's terms with Examples

 

References

https://golang.org/doc/effective_go.html#maps
https://www.golinuxcloud.com/golang-maps/

 

Related Keywords: golang delete key from map, golang delete whole map, golang delete from map while iterating, golang delete all keys from map, golang delete multiple keys from map, golang map, golang map delete key not exist

 

Categories GO

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X