WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Go - Maps


<

Map is a collection of unordered key-value pairs. The most important point of Map is to quickly retrieve data through key, which is similar to an index and points to the value of the data.

Map is a collection, so we can iterate over it like iterating over arrays and slices. However, Map is unordered, and we can't determine its return order because Map is implemented using the hash table.

Defining Map

You can use the built-in function make or you can use the map keyword to define a map:

/* Declare variables, default map is nil */
var map_variable map[key_data_type]value_data_type

/* Use the make function */
Map_variable := make(map[key_data_type]value_data_type)

If you do not initialize map, then a nil map is created. Nil map cannot be used to store key-value pairs

Instance

The following example demonstrates creating and using a map:

example

package main

import "fmt"

func main() {
    var countryCapitalMap map[string]string /*Create collection */
    countryCapitalMap = make(map[string]string)

    /* Map insert key - value pairs, the corresponding capital 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])
    }

    /*See if an 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("American of The capital is", capital)
    } else {
        fmt.Println("American The capital does not exist")
    }
}

The above example runs as follows:

France The capital is Paris
Italy The capital is Rome
Japan The capital is Tokyo
India  The capital is New Delhi
American The capital does not exist
< Hr>

delete() function

The

delete() function is used to remove the elements of the collection, with the parameter map and its corresponding key. An example is as follows:

Instance

package main

import "fmt"

func main() {
        /* Create 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 was deleted")

        fmt.Println("Map after deleting elements")

        /*Print map*/
        for country := range countryCapitalMap {
                fmt.Println(country, "The capital is", countryCapitalMap [ country ])
        }
}

The above example runs as follows:

Original map
India The capital is New delhi
France The capital is Paris
Italy The capital is Rome
Japan The capital is Tokyo
French entry was deleted
Map after deleting elements
Italy The capital is Rome
Japan The capital is Tokyo
India The capital is New delhi