Go language Map (Collection)
Map
is a collection of unordered key-value pairs. The most important point of Map
is passing the key
to retrieve data quickly key
, similar to an index, points to the value of the data.
Map
is a collection, so we can iterate over it like arrays and slices. But, Map
is unordered, and we cannot determine the order in which it isreturned, because Map
is to use the hash
table to achieve.
Define Map
You can use built-in functions make
, you can also use the map
keyword to define Map
:
/* Declare variables, default map is nil */
var map_variable map[key_data_type]value_data_type
/* Using the make function */
map_variable := make(map[key_data_type]value_data_type)
If you don’t initialize map
then a nil map
. nil map
cannot be used to store key-value pairs
Example
The following example demonstrates the creation and use of map
:
Example
package main
import "fmt"
func main() {
var countryCapitalMap map[string]string /*Create Collection */
countryCapitalMap = make(map[string]string)
/* Insert key value pairs into the map, corresponding to
the capital cities of each country */
countryCapitalMap [ "France" ] = "Paris"
countryCapitalMap [ "Italy" ] = "Rome"
countryCapitalMap [ "Japan" ] = "Tokyo"
countryCapitalMap [ "India " ] = "New Delhi"
/*Use the key to output map values */
for country := range countryCapitalMap {
fmt.Println(country, "The capital is", countryCapitalMap [country])
}
/*Check if the element exists in the collection */
capital, ok := countryCapitalMap [ "American" ]
/*If it is determined to be true, then it exists;
otherwise, it does not exist */
/*fmt.Println(capital) */
/*fmt.Println(ok) */
if (ok) {
fmt.Println("The capital of America is", capital)
} else {
fmt.Println("The capital city of America does not exist")
}
}
The running result of the above instance is as follows:
The capital of France is Paris
Italy's capital is Rome
The capital of Japan is Tokyo
India's capital is New Delhi
The capital city of America does not exist
delete()
function
delete()
function is used to delete the elements of the collection, and the argument is map
and its corresponding key
. Examples are as follows:
Example
package main
import "fmt"
func main() {
/* Create a map */
countryCapitalMap := map[string]string{"France": "Paris",
"Italy": "Rome", "Japan": "Tokyo", "India": "New delhi"}
fmt.Println("Original Map")
/* Print Map */
for country := range countryCapitalMap {
fmt.Println(country, "The capital is", countryCapitalMap [
country ])
}
/*Delete Element*/ delete(countryCapitalMap, "France")
fmt.Println("French entry deleted")
fmt.Println("Map after deleting elements")
/*Print Map*/
for country := range countryCapitalMap {
fmt.Println(country, "The capital is", countryCapitalMap [
country ])
}
}
The running result of the above instance is as follows:
Original map
The capital of India is New delhi
The capital of France is Paris
Italy's capital is Rome
Japan's capital is Tokyo
French entry deleted
Map after deleting elements
Italy's capital is Rome
Japan's capital is Tokyo
The capital of India is New delhi