0
0
Goprogramming~10 mins

Why maps are used in Go - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why maps are used
Start
Need to store data
Choose data structure
List/Array
Map
Use map for key-value pairs
Fast lookup, add, delete by key
End
This flow shows why we pick maps: to store data with keys for fast searching and easy access.
Execution Sample
Go
package main
import "fmt"
func main() {
  m := map[string]int{"apple": 5, "banana": 3}
  fmt.Println(m["apple"])
}
This code creates a map with fruit names as keys and numbers as values, then prints the value for "apple".
Execution Table
StepActionMap StateOutput
1Create map with keys "apple"=5, "banana"=3{"apple":5, "banana":3}
2Access value for key "apple"{"apple":5, "banana":3}5
3Program ends{"apple":5, "banana":3}
💡 Program ends after printing the value for key "apple"
Variable Tracker
VariableStartAfter Step 1After Step 2Final
mnil{"apple":5, "banana":3}{"apple":5, "banana":3}{"apple":5, "banana":3}
Key Moments - 2 Insights
Why do we use a map instead of a list or array?
Maps let us find values quickly by their keys without checking every item, as shown in step 2 of the execution_table where we directly get the value for "apple".
What happens if we try to get a value for a key not in the map?
The map returns the zero value for the value type (like 0 for int), but this is not shown in the current execution_table because all keys exist.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of m after step 1?
Anil
B{"apple":5, "banana":3}
C{}
D{"apple":3, "banana":5}
💡 Hint
Check the 'Map State' column in row for step 1 in execution_table
At which step does the program print the value 5?
AStep 2
BStep 3
CStep 1
DNo step prints 5
💡 Hint
Look at the 'Output' column in execution_table rows
If we add a new key "orange" with value 7, what changes in variable_tracker?
Am becomes nil
Bm loses all previous keys
Cm changes to include "orange":7 after the step it is added
DNo change to m
💡 Hint
Variable_tracker shows how m changes after each step
Concept Snapshot
Maps store data as key-value pairs.
They let you find, add, or remove values fast by key.
Use maps when you want quick lookup, not just a list.
In Go, maps are created with map[keyType]valueType.
Access values by m[key], get zero if key missing.
Full Transcript
Maps are used to store data where each item has a unique key. This helps find data quickly without searching through everything. In Go, you create a map with keys and values. For example, a map of fruits to numbers. When you ask for a value by key, the map gives it fast. If the key is missing, it returns zero for the value type. This makes maps very useful for fast lookups and organizing data by keys.