Get the App
SLTechnology News&Howtos  ›  Development  › 

How to create and use map in Go language

Shulou Source: shulou.com Published: 2022-06-01 02:26:48 10月03日 Update

This article mainly introduces "how to create and use map in Go language". In daily operation, I believe that many people have doubts about how to create and use map in GE language. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to create and use map in Go language". Next, please follow the editor to study!

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, about "how to create and use map in the Go language" study is over, hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Capital language learning examples more Tokyo Paris New Delhi Rome help practical next elements countries maps articles methods theory knowledge articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MySQL vpn Huawei Redmi