Introduction
In Go, maps are collections of key-value pairs where each key is unique. You can delete a key (and its associated value) from a map using the delete function. This guide will demonstrate how to delete a key from a map in Go.
Problem Statement
Create a Go program that:
- Declares and initializes a map with some key-value pairs.
- Deletes a specified key from the map.
- Displays the map before and after deleting the key.
Example:
- Input: Map:
{"Apple": 5, "Banana": 3, "Cherry": 7}, Key to delete:"Banana" - Output:
Map before deletion: map[Apple:5 Banana:3 Cherry:7] Map after deletion: map[Apple:5 Cherry:7]
Solution Steps
- Import the fmt Package: Use
import "fmt"for formatted I/O operations. - Write the Main Function: Define the
mainfunction, which is the entry point of every Go program. - Create and Initialize the Map: Use a map literal to create and initialize a map with some key-value pairs.
- Delete a Key from the Map: Use the
deletefunction to remove the specified key from the map. - Display the Map: Use
fmt.Printlnto display the map before and after deleting the key.
Go Program
package main
import "fmt"
/**
* Go Program to Delete a Key from a Map
* Author: https://www.javaguides.net/
*/
func main() {
// Step 1: Create and initialize the map
fruitMap := map[string]int{
"Apple": 5,
"Banana": 3,
"Cherry": 7,
}
// Display the map before deletion
fmt.Println("Map before deletion:", fruitMap)
// Step 2: Delete the specified key from the map
delete(fruitMap, "Banana")
// Step 3: Display the map after deletion
fmt.Println("Map after deletion:", fruitMap)
}
Explanation
Step 1: Create and Initialize the Map
- The map is created and initialized using a map literal. For example,
fruitMap := map[string]int{"Apple": 5, "Banana": 3, "Cherry": 7}creates a map with three key-value pairs.
Step 2: Delete the Specified Key from the Map
- The
deletefunction is used to remove a key from the map. The syntaxdelete(map, key)is used. For example,delete(fruitMap, "Banana")removes the key"Banana"and its associated value from the map.
Step 3: Display the Map
- The program prints the map using
fmt.Printlnto display its contents before and after the key has been deleted.
Output Example
Example 1:
Map before deletion: map[Apple:5 Banana:3 Cherry:7]
Map after deletion: map[Apple:5 Cherry:7]
Example 2:
If you try to delete a key that doesn’t exist:
delete(fruitMap, "Mango") // "Mango" does not exist in the map
fmt.Println("Map after attempting to delete non-existent key:", fruitMap)
Output:
Map after attempting to delete non-existent key: map[Apple:5 Banana:3 Cherry:7]
Example 3:
Starting with a larger map:
fruitMap := map[string]int{
"Apple": 5,
"Banana": 3,
"Cherry": 7,
"Mango": 10,
"Orange": 8,
}
delete(fruitMap, "Mango")
fmt.Println("Map after deleting 'Mango':", fruitMap)
Output:
Map after deleting 'Mango': map[Apple:5 Banana:3 Cherry:7 Orange:8]
Conclusion
This Go program demonstrates how to delete a key from a map using the delete function. It covers basic programming concepts such as map manipulation, key removal, and map state before and after modification. This example is useful for beginners learning Go programming and understanding how to manage and modify maps effectively.