YouTip LogoYouTip

Go Map

\n\n\n\nGo Language Map (Collection) |\n\n\n\n

Go Language Map (Collection)

\n\n

Map is a collection of unordered key-value pairs.

\n

The most important thing about Map is to quickly retrieve data through keys, which are like indexes pointing to the values of the data.

\n

Map is a collection, so we can iterate over it like arrays and slices. However, Map is unordered, and the order of key-value pairs returned when traversing a Map is uncertain.

\n

When getting a value from a Map, if the key does not exist, the zero value of that type is returned. For example, the zero value for int is 0, and for string it is "".

\n

Map is a reference type. If a Map is passed to a function or assigned to another variable, they all point to the same underlying data structure, so modifications to the Map will affect all variables that reference it.

\n\n

Defining a Map

\n

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

\n
\n/* Using make function */ \nmap_variable := make(mapValueType, initialCapacity)\n// KeyType is the type of the key, ValueType is the type of the value, initialCapacity is an optional parameter used to specify the initial capacity of the Map. The capacity of a Map refers to the number of key-value pairs it can hold. When the number of key-value pairs reaches the capacity, the Map will automatically expand. If initialCapacity is not specified, Go will choose an appropriate value based on the actual situation.\n
\n\n

Example

\n
\n// Create an empty Map\n m :=make(mapint)\n\n// Create a Map with an initial capacity of 10\n m :=make(mapint,10)\n
\n

You can also create a Map using literals:

\n
\n// Create a Map using literals \nm := mapint{ "apple": 1, "banana": 2, "orange": 3,}\n
\n

Get an element:

\n
\n// Get a key-value pair\nv1 := m \nv2, ok := m // If the key does not exist, ok is false, v2 is the zero value of that type\n
\n

Modify an element:

\n
\n// Modify a key-value pair\nm = 5\n
\n

Get the length of Map:

\n
\n// Get the length of Map\nlen := len(m)\n
\n

Traverse the Map:

\n
\n// Traverse the Map\nfor k, v := range m { \n\tfmt.Printf("key=%s, value=%dn", k, v)\n}\n
\n

Delete an element:

\n
\n// Delete a key-value pair\ndelete(m, "banana")\n
\n\n

Example

\n

The following example demonstrates creating and using a map:

\n\n

Example

\n
\npackage main\n\nimport "fmt"\n\nfunc main() {\n    var siteMap mapstring /* Create a collection */\n    siteMap = make(mapstring)\n\n    /* Insert key-value pairs into map, corresponding to countries and their capitals */\n    siteMap  = "Google"\n    siteMap  = ""\n    siteMap  = "Baidu"\n    siteMap  = "Wikipedia"\n\n    /* Output map values using keys */\n    for site := range siteMap {\n        fmt.Println(site, "The capital is", siteMap )\n    }\n\n    /* Check if an element exists in the collection */\n    name, ok := siteMap  /* If confirmed true, it exists; otherwise not */\n    if (ok) {\n        fmt.Println("Facebook The site is", name)\n    } else {\n        fmt.Println("Facebook The site does not exist")\n    }\n}\n
\n

The output of the above example is:

\n
\nWiki The capital is Wikipedia\nGoogle The capital is Google\n The capital is \nBaidu The capital is Baidu\nFacebook The site does not exist\n
\n\n
\n\n

delete() Function

\n

The delete() function is used to delete elements from a collection. Its parameters are the map and its corresponding key. Example:

\n\n

Example

\n
\npackage main\n\nimport "fmt"\n\nfunc main() {\n    /* Create a map */\n    countryCapitalMap := mapstring{"France": "Paris", "Italy": "Rome", "Japan": "Tokyo", "India": "New delhi"}\n    fmt.Println("Original map")\n\n    /* Print the map */\n    for country := range countryCapitalMap {\n        fmt.Println(country, "The capital is", countryCapitalMap )\n    }\n\n    /* Delete element */ \n    delete(countryCapitalMap, "France")\n    fmt.Println("The French entry was deleted")\n    fmt.Println("After deleting elements, the map")\n\n    /* Print the map */\n    for country := range countryCapitalMap {\n        fmt.Println(country, "The capital is", countryCapitalMap )\n    }\n}\n
\n

The output of the above example is:

\n
\nOriginal map\nFrance The capital is Paris\nItaly The capital is Rome\nJapan The capital is Tokyo\nIndia The capital is New delhi\nThe French entry was deleted\nAfter deleting elements, the map\nItaly The capital is Rome\nJapan The capital is Tokyo\nIndia The capital is New delhi\n
\n\n \n
← Postgresql Sub QueriesPostgresql Transaction β†’