Home » How to Delete a Key from a Map in Golang

How to Delete a Key from a Map in Golang

Use the Golang built-in delete() function to delete a key from a map in Golang. Golang map stores data in key-value pairs. If the key gets deleted, it will remove its value as well.

If the map is nil, the delete() function will have no effect.

delete() function takes two arguments, the map and the key which is to be removed from the map.

delete( map, key)

In this article, we will discuss how to delete a key from a map in golang.

How to Delete a Key from Map by key in Golang

Use the delete() function in go to delete a key from a map based on a specified key in golang.

Refer to the following program to remove a key from a map.

package main

import (
	"fmt"
)

func main() {

	// Create a map in go using the make function

	score := make(map[string]int)

	// Add elements to map
	score["Alex"] = 94
	score["Gary"] = 75
	score["Mary"] = 67

	// Print the original map
	fmt.Println(score)

	// To delete a key from the map
	// Use delete() function and pass map and key

	delete(score, "Gary")

	// Print the map after removing the key
	fmt.Println(score)
}

The output of the above Golang program to remove a key from a map based on a specified key is:

map[Alex:94 Gary:75 Mary:67]

map[Alex:94 Mary:67]

To delete a key from a map in Go, follow the below steps:

  1. Create a map using the make() function
  2. Add elements to the map, e.g. score["Alex"] = 94
  3. Use the delete() function to remove a key from a map
  4. Pass map object and key in delete() function, e.g. delete(score,"Gary")
  5. it deletes a key from a map as well as its value.
  6. Use Println() to print the updated map.

Cool Tip: How to get a slice of keys from a map in Golang!

How to Remove a Key from the Map in Golang

To delete a key from a map, we use the delete() function.

delete() function takes two arguments; map and key. If the key doesn’t exist in the map, the delete() function has no effect.

However, we should check if the key exists in the go map before we call the delete() function.

package main

import (
	"fmt"
)

func main() {

	// Create a map in go using the make function

	score := make(map[string]int)

	// Add elements to map
	score["Alex"] = 94
	score["Gary"] = 75
	score["Mary"] = 67

	// Print the original map
	fmt.Println(score)

	// To delete a key from the map
	// Use delete() function and pass map and key

	if _, found := score["Sam"]; found {
		delete(score, "Sam")
	} else {
		fmt.Println("Key doesn't exists")
	}

}

The output of the above Golang program is:

map[Alex:94 Gary:75 Mary:67]

Key doesn't exists

In the above program, it checks if the key exists in the map and if it’s found then only it calls the delete() method to remove a key from a map

Conclusion

I hope the above article on how to delete a key from a map in golang using the delete() function is helpful to you.

You can find more topics about the Golang tutorials on the GolangSpot Home page.

Leave a Comment