Go Maps
Maps are Go's built-in associative data type (sometimes called hashes or dicts in other languages). They store data in key-value pairs.
1. Basic Usage
To create an empty map, use the built-in make function.
2. Map Literals
You can declare and initialize a new map on the same line.
3. Checking if a Key Exists
When you read from a map, it returns two values: the value and a boolean indicating if the key was actually present.
Important Notes
- Zero Value: If a key doesn't exist, Go returns the zero value for the value type (e.g.,
0for anintmap). - Unordered: Maps are inherently unordered. If you iterate over a map multiple times, the order of keys is not guaranteed to be the same.
- Reference Type: Like slices, maps are passed by reference.