Introduction to Maps
A map is a built in Go data structure that stores information as key value pairs .
Unlike arrays and slices, where elements are accessed using their positions (indexes), values in a map are accessed using their keys.
Every entry in a Go map consists of two things:
Key: An identifier used to find an entry in the map.
Value: The information associated with that key.
For example, imagine storing information about a user. We could use the user's name and email as key value pairs.
user := map [ string ] string { "name" : "Adetola" , "email" : "[email protected]" , }
Enter fullscreen mode
Exit fullscreen mode
syntax
The general syntax for declaring a map is:
map [ keyType ] valueType
Enter fullscreen mode
Exit fullscreen mode
In our example:
map [ string ] string
Enter fullscreen mode
Exit fullscreen mode
map tells Go that we are declaring a map.
The first string specifies the data type of the keys.
The second string specifies the data type of the values.
This means we…