How to create and use map in Go
Most people do not understand the knowledge points of this article "how to create and use map in Go", so the editor summarizes the following contents, detailed contents, clear steps, and certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to create and use map in Go" article.
Example
Package main
Import "fmt"
Func main () {
Var countryCapitalMap map [string] string / * create collection * /
CountryCapitalMap = make (map [string] string)
/ * map inserts key-value pairs, corresponding capitals of each country * /
CountryCapitalMap ["France"] = "Paris"
CountryCapitalMap ["Italy"] = "Rome"
CountryCapitalMap ["Japan"] = "Tokyo"
CountryCapitalMap ["India"] = "New Delhi"
/ * use keys to output map values * /
For country: = range countryCapitalMap {
Fmt.Println (country, "Capital is", countryCapitalMap [country])
}
/ * check whether the element exists in the collection * /
Capital, ok: = countryCapitalMap ["American"] / * if it is determined to be true, it exists, otherwise it does not exist * /
/ * fmt.Println (capital) * /
/ * fmt.Println (ok) * /
If (ok) {
Fmt.Println ("Capital of American", capital)
} else {
Fmt.Println ("Capital of American does not exist")
}
}
The running result of the above instance is as follows:
France Capital is Paris Italy Capital is Rome Japan Capital is Tokyo India Capital is New Delhi American Capital does not exist above is about "how to create and use map in Go" the content of this article, I believe you all have a certain understanding, I hope the content shared by the editor will be helpful to you, if you want to know more related knowledge content, please follow the industry information channel.