23. Maps
Let's navigate!
Map Type
Map is collection of key value pairs. It goes by various names in other programming languages like dictionaries in Python, objects in JavaScript etc.
Maps are represented as map[K]V
, where K
and V
are the types of its keys and values.
Declaring Maps
We can declare map
using var declaration. We are defining string
to be keys for the map and string
to be values. Since map is declared but not initialized with any value, it will be a nil
map.
Creating map
Maps are created using make
function call. make
will perform the required memory allocation required to use map. We can add elements to map using map[key] = value
semantics.
Map Literals
We can also use map literal to declare and initialize maps. make
function call is not required when using map literal.
Accessing map elements
map elements can be accessed using subscript notion similar to array and slices. In array and slices, index can only be integers. For maps, however, key can be anything which can be compared using ==
comparison.
If we try to access map key which does not exists, it will not cause an error, but will return zero value.
Zero values for map
Zero value for map is nil. Zero value for map element is zero value for type being stored as value.
If map is not nil, but empty, accessing its elements will returns its zero value.
Notice that, event if key does to exist in empty map, it did return zero value.
Nil and empty map
Similar to slices, maps can be empty or nil. When we use make function call or use map literal for declaring map, it initializes empty map
Both nil
and empty maps have len()
as zero.
Iterating over map
Iterating over maps is similar to array and slices, we can use for...range
loop for iteration. range
works as follows for a map
for key,value :=range map { // }
Unlike arrays and slices, maps do not guarantee same order. Map is unordered collection of key-value pairs.
If either key or value is not required, we can omit it by using blank identifier.
Finding element in map
To find element in map, we provide key to the map to get element/value returned from map. A map does not panic if we try to access key which is not present, instead it returns zero value. To identify if value is returned is actual value present on map or zero value returned due to absence of key, map returns a second value, ok
which tells if key was found or not.
If we see output for below code snippet, when we check for XYZ
and ABC
, both returns empty string. For XYZ
, value is empty string, while for ABC
, it is missing from map.
To distinguish between empty/zero value and missing value, we can use value,ok
semantics.
Updating element in map
Updating value is similar to creating a value. We can use
map[key]= value
semantic to update value for given key.
Deleting element from map
Deleting value from map is straightforward. Go provides inbuilt delete function to delete a value from map. Semantics for delete is
delete(mapName,key)
Comparing maps
Similar to slices, maps can not be compared directly. We can only check if a map is nil or not directly. For other comparisons, we need to write our own for loop for comparison.
GitHub Code
Blog Posts
Videos
Last updated