Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




GO - REMOVE FROM MAP


How to remove an element from the Map?


Let us say, we have a Map that contains,

Pune, Is a City
John, Is a Name
Go, Is a Language

As 'Key' and 'Value' pair.


Now, if you want to remove the entries from a 'Map', 'pop()', 'popitem()', 'clear()' Function and 'del' keyword can be used.


Let us look at the 'pop()' Function first.


How to remove an element using the delete() Function?


The 'delete()' Function can be used to remove an item from the Map.


Let us say, we want to remove the entry where the 'Key' is 'Pune' and 'Value' is 'Is a City'.

Pune, Is a City in India


package main
import "fmt"
    
func main() {
    
    x := map[string]string {
        "Pune" : "Is a City", 
        "John": "Is a Name", 
        "Go": "Is a Language"}
            
    delete(x, "Pune")
        
    for i,j := range x {
        fmt.Println("The value for the key ",i," is ",j)
    }				 	
}


Output :



 The value for the key John is Is a Name
 The value for the key Go is Is a Language

So, in the above code we have created a 'Map' using braces '{}' and 'Key' and 'Value' pairs.


x := map[string]string {
	"Pune" : "Is a City", 
	"John": "Is a Name", 
	"Go": "Is a Language"}

And initialised to the variable 'x'.


java_Collections

Now, we are supposed to delete the value of the 'Key', 'Pune'.


So, we have used the 'delete()' Function to delete it.


delete(x, "Pune")

And the entry for the 'Key' 'Pune' is deleted for the 'Map'.


java_Collections

And we get the below output,


Output :



 The value for the key John is Is a Name
 The value for the key Go is Is a Language