0
0
Goprogramming~10 mins

Map use cases in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Map use cases
Declare map variable
Initialize map with make()
Add key-value pairs
Access values by key
Check if key exists
Delete key-value pair
Iterate over map
End
This flow shows how to declare, initialize, use, and manipulate maps step-by-step in Go.
Execution Sample
Go
package main
import "fmt"
func main() {
  m := make(map[string]int)
  m["apple"] = 5
  fmt.Println(m["apple"])
}
This code creates a map, adds a key-value pair, and prints the value for the key "apple".
Execution Table
StepActionMap StateOutput
1Declare map variable mm = nil
2Initialize map with makem = empty map {}
3Add key "apple" with value 5m = {"apple": 5}
4Print m["apple"]m = {"apple": 5}5
5Access m["banana"] (not set)m = {"apple": 5}0 (zero value)
6Check if "banana" existsm = {"apple": 5}false
7Add key "banana" with value 3m = {"apple": 5, "banana": 3}
8Delete key "apple"m = {"banana": 3}
9Iterate over map keys and valuesm = {"banana": 3}banana: 3
10End of programm = {"banana": 3}
💡 Program ends after iterating and printing map contents.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 7After Step 8Final
mnil{}{"apple": 5}{"apple": 5, "banana": 3}{"banana": 3}{"banana": 3}
Key Moments - 3 Insights
Why does accessing a key that doesn't exist return 0 instead of an error?
In Go, accessing a map with a missing key returns the zero value for the value type (here int is 0). See step 5 in execution_table.
How do we check if a key exists in the map?
Use the two-value assignment: value, ok := m[key]. The 'ok' is true if key exists. See step 6 in execution_table.
What happens when we delete a key from the map?
The key-value pair is removed, so the map no longer contains that key. See step 8 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what is the output when accessing m["banana"]?
A0
Bnil
Cerror
Dbanana
💡 Hint
Check the Output column at step 5 in execution_table.
At which step is the key "apple" removed from the map?
AStep 3
BStep 7
CStep 8
DStep 9
💡 Hint
Look at the Map State column to see when "apple" disappears.
If we add m["orange"] = 10 at step 7, what will be the map state after step 7?
A{"banana": 3, "orange": 10}
B{"apple": 5, "banana": 3, "orange": 10}
C{"apple": 5, "orange": 10}
D{"orange": 10}
💡 Hint
Adding a new key adds it alongside existing keys, see variable_tracker after step 7.
Concept Snapshot
Map use cases in Go:
- Declare with var or :=
- Initialize with make(map[keyType]valueType)
- Add/update with m[key] = value
- Access with m[key], returns zero if missing
- Check existence: val, ok := m[key]
- Delete with delete(m, key)
- Iterate with for k, v := range m
Full Transcript
This visual execution trace shows how to use maps in Go. First, we declare a map variable which starts as nil. Then we initialize it with make to create an empty map. We add key-value pairs by assigning values to keys. Accessing a key returns its value or zero if the key is missing. We can check if a key exists using the two-value assignment. Deleting a key removes it from the map. Finally, we can iterate over the map to access all keys and values. The variable tracker shows how the map changes after each step. Key moments clarify common confusions like zero values and key existence checks. The quiz tests understanding of map behavior at specific steps.