WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Go - Maps


Go provides some other important data type named map which maps unique keys to values. A key is an item that you use to retrieve a cost at a later date.

Given a key and a value, you can store the value in a Map object.

Defining a Map

/* declare a variable, via default map will be nil*/
var map_variable map[key_data_type]value_data_type

/* define the map as nil map cannot be assigned any value*/
map_variable = make(map[key_data_type]value_data_type)

Example

The following instance illustrates the way to create and use a map −
bundle primary

import "fmt"

func main() 
   var countryCapitalMap map[string]string
   /* create a map*/
   countryCapitalMap = make(map[string]string)
   
   /* insert key-fee pairs inside the map*/
   countryCapitalMap["USA"] = "Washington D.C."
   countryCapitalMap["Italy"] = "Rome"
   countryCapitalMap["Japan"] = "Tokyo"
   countryCapitalMap["India"] = "New Delhi"
   
   /* print map using keys*/
   for united states of america := variety countryCapitalMap 
      fmt.Println("Capital of",usa,"is",countryCapitalMap[country])
   
   
   /* take a look at if access is gift within the map or now not*/
   capital, adequate := countryCapitalMap["United kingdom"]
   
   /* if adequate is true, entry is gift in any other case entry is absent*/
   if(ok)
      fmt.Println("Capital of United country is", capital)  
    else 
      fmt.Println("Capital of United nation is not gift") 
   


Output

$pass run foremost.Cross
Capital of USA is Washington D.C.
Capital of Italy is Rome
Capital of Japan is Tokyo
Capital of India is New Delhi
Capital of United country isn't always present