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




GO - UPDATE MAP


How to update the entries of a 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, let us say, we want to update the value of the 'Key', 'Pune' with 'Is a City in India'.

Pune, Is a City in India

Where 'Pune' is the 'Key' and 'Is a City in India' is the new 'Value'.


Let us see in the below example.


Example :



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


Output :



 The value for the key Pune is Is a City in India
 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 update the value of the 'Key', 'Pune' with value 'Is a City in India'.


So, we have used the below way to update it.


x["Pune"] = "Is a City in India"

java_Collections

And the entry for the 'Key' 'Pune' is updated in the 'Map' with the new value, 'Is a City in India'.


java_Collections

And we get the below output,


Output :



 The value for the key Pune is Is a City in India
 The value for the key John is Is a Name
 The value for the key Go is Is a Language