Go - Maps
Go provides some other important statistics kind named map which maps particular keys to values. A key is an item that you use to retrieve a cost at a later date.
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)
ExampleThe 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")
$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