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.
You can use built-in functions If you don’t initialize The following example demonstrates the creation and use of The running result of the above instance is as follows: The running result of the above instance is as follows:
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. 2.39.1. Define Map #
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)
map
then a
nil
map
.
nil
map
cannot be used to store key-value pairs 2.39.2. Example #
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 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
2.39.3.
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 ])
}
}
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