A Slice of Maps
This lesson discusses how to make slices of maps.
We'll cover the following
Explanation
Suppose we want to make a slice of maps. We must use make()
two times, first for the slice, then for each of the map-elements of the slice. To access a specific key-value pair from a map, you have to use an iterator to specify which map from the slice of maps is required.
For example, if we have a slice of maps called maps
, and we want to set a value v
with key 1
from map i
, we’ll do something as follows:
maps[i][1] = v
Simply writing maps[1] = v
won’t work, because it will initialize map-variables, and will be lost on the next iteration.
Look at the following implementation of how to make a slice of maps in Go.
Get hands-on with 1400+ tech skills courses.