Mahdi.Kh
September 21, 2026
Maps are a data structure for storing key/value data. In this part, we get familiar with maps and learn how to use them.
map is a data structure for storing data that comes as key/value pairs. In key/value data, we assign a value to a key, and later, using that key, we can access its value.
To define a map, we need to use the map keyword:
Inside the square brackets, [ ], we specify the keys' data type, and after the brackets, we specify the values' data type. In the example above, we created a map whose keys and values can be of string type.
The keys' data type must be comparable. You can't use a data type that can't be compared with the == operator as the key type. For example, you can't use a slice as a key.
In the example above, the keys' data type is string and the values' data type is int.
The map data type is a reference type (similar to a pointer or slice); after declaring a map, its zero value will be nil. In this state, you can read a value from the map, but you can't write anything onto the map! Take a look at the example below:
If a map's value is nil, reading a value from it returns the default value (zero value) corresponding to the value type; in this state, trying to write to the map results in a runtime panic. So you should never try to write data into a map before allocating memory for it (initializing the map).
So far, we've gotten familiar with declaring a map and learned that its initial value is nil. There are two ways to allocate memory for a map:
A map literal
Using the make function
A map literal means using the map keyword to create a value of map type and put it into your variable.
You can also use make to allocate memory for a map, in which case the map's initial value will no longer be nil, and you'll be able to write values onto the map.
In a map, if a key doesn't exist, the zero value corresponding to its value type is returned. Using a second variable, we can check whether the returned value is actually there.
As we saw in the example above, if the key we want has been assigned a value, ok will be true.
To delete a key from a map, we can use the delete function. This function does nothing if the key we want doesn't exist. Take a look at the example below:
To display the values inside a map, you can also use a for-range loop:
When we iterate over a map using a for-range loop, the data is read in a different order each time. If you run the code above several times, the order of the data shown in the output will be different each time.
To be able to display the data in order, we can get help from a slice:
In a slice, data is read according to the order of its indexes. So we can put a map's keys into a slice in whatever order we want, and then, by iterating over the values in this slice, get the map's keys and read and display each key's corresponding value from the map. In the example above, we sort the keys alphabetically using the sort.Strings function, and then display them in that order.
Previous part: Loops | Golang basics
Next part: Functions | Golang basics