YouTip LogoYouTip

Go Map

# Go Language Map (Collection) A Map is an unordered collection of key-value pairs. The most important aspect of a Map is the ability to quickly retrieve data using a key, which acts like an index pointing to the value. A Map is a collection, so we can iterate over it like we do with arrays and slices. However, Maps are unordered, meaning the order of key-value pairs returned when iterating over a Map is not guaranteed. When retrieving a value from a Map, if the key does not exist, the zero value of the value's type is returned. For example, the zero value for an `int` is `0`, and for a `string` it is `""`. Maps are reference types. If you pass a Map to a function or assign it to another variable, they both point to the same underlying data structure. Therefore, modifications to the Map will affect all variables that reference it. ### Defining a Map You can define a Map using the built-in `make` function or the `map` keyword: /* Using the make function */ map_variable := make(mapValueType, initialCapacity) Where `KeyType` is the type of the key, `ValueType` is the type of the value, and `initialCapacity` is an optional parameter to specify the initial capacity of the Map. The capacity of a Map is 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 a suitable value based on the actual situation. ## Example // Create an empty Map m :=make(mapint) // Create a Map with an initial capacity of 10 m :=make(mapint,10) You can also create a Map using a literal: // Create a Map using a literal m := mapint{ "apple": 1, "banana": 2, "orange": 3,} Retrieve elements: // Retrieve a key-value pair v1 := m v2, ok := m // If the key does not exist, ok is false, and v2 is the zero value of the type Modify elements: // Modify a key-value pair m = 5 Get the length of the Map: // Get the length of the Map len := len(m) Iterate over the Map: // Iterate over the Mapfor k, v := range m { fmt.Printf("key=%s, value=%dn", k, v)} Delete elements: // Delete a key-value pairdelete(m, "banana") ### Example The following example demonstrates creating and using a map: ## Example package main import"fmt" func main(){ var siteMap mapstring/*Create a collection */ siteMap =make(mapstring) /* Insert key-value pairs into the map, mapping countries to their capitals */ siteMap ="Google" siteMap ="" siteMap ="Baidu" siteMap ="Wikipedia" /* Output map values using keys */ for site :=range siteMap { fmt.Println(site,"capital is", siteMap ) } /* Check if an element exists in the collection */ name, ok := siteMap /*If it exists, ok is true; otherwise, it's false */ /*fmt.Println(capital) */ /*fmt.Println(ok) */ if(ok){ fmt.Println("Facebook's site is", name) }else{ fmt.Println("Facebook site does not exist") } } The output of the above example is: Wiki capital is WikipediaGoogle capital is GoogleTutorial capital is Baidu capital is BaiduFacebook site does not exist * * * ## delete() Function The `delete()` function is used to delete elements from a collection. Its parameters are the map and the corresponding key. Example: ## Example package main import"fmt" func main(){ /* Create a map */ countryCapitalMap :=mapstring{"France":"Paris","Italy":"Rome","Japan":"Tokyo","India":"New delhi"} fmt.Println("Original map") /* Print the map */ for country :=range countryCapitalMap { fmt.Println(country,"capital is", countryCapitalMap ) } /* Delete an element */ delete(countryCapitalMap,"France") fmt.Println("France entry deleted") fmt.Println("Map after deletion") /* Print the map */ for country :=range countryCapitalMap { fmt.Println(country,"capital is", countryCapitalMap ) } } The output of the above example is: Original mapIndia capital is New delhi France capital is ParisItaly capital is RomeJapan capital is TokyoFrance entry deletedMap after deletionItaly capital is RomeJapan capital is TokyoIndia capital is New delhi
← Go InterfacesGo Recursion β†’