Go Maps
Go Language- Maps
Map is an another important data type which maps unique keys to values. A key is used to retrieve a value at the later date. The value can be stored in map object by giving a key and a value. After that that value can retrieved by using its key. By default,map will be nil.
Map definition :
Make function is used to create a map.
Syntax
/* declare a variable, by default map will be nil*/
var map_variable map[key_data_type]value_data_type
/* define the map as nil map can not be assigned any value*/
map_variable = make(map[key_data_type]value_data_type)
Program Example :
CODE/PROGRAM/EXAMPLE
package main
import "fmt"
func main(){
var countryCapitalMap map[string]string
/* create a map*/
countryCapitalMap =make(map[string]string)
/* insert key-value pairs in the map*/
countryCapitalMap["France"]="Paris"
countryCapitalMap["Pakistan"]="Islamabad"
countryCapitalMap["Japan"]="Tokyo"
countryCapitalMap["India"]="New Delhi"
/* print map using keys*/
for country :=range countryCapitalMap {
fmt.Println("Capital of",country,"is",countryCapitalMap[country])
}
/* test if entry is present in the map or not*/
capital,ok :=countryCapitalMap[
"United States"]
/* if ok is true, entry is present otherwise entry is absent*/
if(ok){
fmt.Println("Capital of United States is",capital)
}
else{
fmt.Println("Capital of United States is not present")
}
}
Output:
Capital of India is New Delhi
Capital of France is Paris
Capital of Pakistan is Islamabad
Capital of Japan is Tokyo
Capital of United States is not present
Delete function :
The delete() function is used to delete an entry from a map. For this it needs the map and the respective key which is to deleted.
Program Example :
CODE/PROGRAM/EXAMPLE
package main
import"fmt"
func main(){
/* create a map*/
countryCapitalMap :=map[string]string{
"France":"Paris","Pakistan":"Islamabad","Japan":"Tokyo","India":"New Delhi"
}
fmt.Println("Original map")
/* print map */
for country :=range countryCapitalMap {
fmt.Println("Capital of",country,"is",countryCapitalMap[country])
}
/* delete an entry */
delete(countryCapitalMap,"France");
fmt.Println("Entry for France is deleted")
fmt.Println("Updated map")
/* print map */
forcountry :=range countryCapitalMap {
fmt.Println("Capital of",country,"is",countryCapitalMap[country])
}
}
Output :
Original Map
Capital of France is Paris
Capital of Pakistan is Islamabad
Capital of Japan is Tokyo
Capital of India is New Delhi
Entry for France is deleted
Updated Map
Capital of India is New Delhi
Capital of Pakistan is Islamabad
Capital of Japan is Tokyo
Map operations :
For inserting or updating an element in map the following format is used. Consider the map m.
m[key] = elem /*map name[key]= value*/
For retrieving anelement, the below given format is used.
elem = m[key] /*variable to assigned with the retrieved value = map name[key] is used to retrieve the value*/
Test that a key is present with a two-value assignment elem, ok = m[key]
If key is in m,ok is true. If not,ok is false. If key is not in the map, then elem is the zero value for the map's element type.
CODE/PROGRAM/EXAMPLE
func main() {
m := make(map[string]int)
m["Answer"] = 42
fmt.Println("The value:", m["Answer"])
m["Answer"] = 48
fmt.Println("The value:", m["Answer"])
delete(m, "Answer")
fmt.Println("The value:", m["Answer"])
v, ok := m["Answer"]
fmt.Println("The value:", v, "Present?", ok)
}
Note : If element or ok have not yet been declared you could use a short declaration form: element,
ok := m[key]