# Basics of `map`tutorial

``````map[KeyType]ValueType
``````
• Maps are reference types, so you don't need to pass maps as pointers.
• The `KeyType` must be comparable using `==`.
• So, types like `slice` cannot be a `KeyType`.
• The zero value for a `map` is `nil`.
• `nil` `map` behaves like an empty map. Most operations without assignments are safe.
Declare / init a `map`
``````var m map[string]int
// or
m := map[string]int{}
// or
m := make(map[string]int)
// or (with initial values)
ages := map[string]int{
"yeonghoey": 32,
}``````
Iterate over a `map`
``````m := map[string]int
for key := range m {}
for key, value := range m {}
for _, value := range m {}``````
Get an element in a `map`
``````x, ok := m["key"]
// or concisely
if x, ok := m["key"]; !ok {
}``````
• The second value `ok` is a `bool` that is `true` if the key exists in the map.
• When `ok` is `false`, `x` is going to be the zero value of the `ValueType`.
• As a `map` element is not a variable, you cannot take its address.
Delete(remove) an element from a `map`
``delete(m, "route")``
• `delete()` doesn't return anything.
• `delete()` will do nothing if the specified key doesn't exist.

# Mimic `set` type with a `map`howto

``````m := make(map[string]bool)
if !m[x] {
m[x] = true
}``````

# Implement a nested `map` which works like a `defaultdict` in Python howto

``````graph := make(map[string]map[string]bool)