0
0
Goprogramming~10 mins

Accessing map values in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Accessing map values
Declare map with key-value pairs
Access value using key
Check if key exists?
NoHandle missing key
Yes
Use the value
This flow shows how to get a value from a map by key, check if the key exists, and then use the value or handle the missing key.
Execution Sample
Go
package main
import "fmt"
func main() {
  m := map[string]int{"a": 1, "b": 2}
  val, ok := m["a"]
  fmt.Println(val, ok)
}
This code creates a map, accesses the value for key "a", and prints the value and whether the key exists.
Execution Table
StepActionExpressionResultNotes
1Declare mapm := map[string]int{"a":1, "b":2}m = {"a":1, "b":2}Map created with keys "a" and "b"
2Access value and check keyval, ok := m["a"]val=1, ok=trueKey "a" exists, value is 1
3Print valuesfmt.Println(val, ok)Output: 1 truePrints value and key existence
4Access missing keyval2, ok2 := m["c"]val2=0, ok2=falseKey "c" does not exist, val2 is zero value
5Print missing key resultfmt.Println(val2, ok2)Output: 0 falseShows missing key returns zero and false
💡 Execution ends after printing values for existing and missing keys.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
mnil{"a":1, "b":2}{"a":1, "b":2}{"a":1, "b":2}{"a":1, "b":2}
valundefinedundefined111
okundefinedundefinedtruetruetrue
val2undefinedundefinedundefined00
ok2undefinedundefinedundefinedfalsefalse
Key Moments - 2 Insights
Why does accessing a missing key return 0 instead of an error?
In Go, accessing a map with a missing key returns the zero value of the map's value type (here int is 0) and false for the existence check, as shown in step 4 and 5.
What does the 'ok' variable represent when accessing a map?
The 'ok' variable is a boolean that tells if the key exists in the map. If true, the key was found (step 2); if false, it was missing (step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'ok' after step 2?
Aundefined
Bfalse
Ctrue
D0
💡 Hint
Check the 'Result' column for step 2 in the execution_table.
At which step does the program access a key that does not exist in the map?
AStep 2
BStep 4
CStep 1
DStep 3
💡 Hint
Look for the step where 'ok' is false in the execution_table.
If the map had a key "c" with value 5, what would 'val2' be after step 4?
A5
B0
Cfalse
Dundefined
💡 Hint
Refer to variable_tracker values for 'val2' and consider the map contents.
Concept Snapshot
Accessing map values in Go:
- Use val, ok := m[key] to get value and check existence.
- If key exists, ok is true and val is the value.
- If key missing, ok is false and val is zero value.
- Always check ok to avoid confusion with zero values.
- This helps safely handle map lookups.
Full Transcript
This visual trace shows how to access values in a Go map. First, a map m is declared with keys "a" and "b". Then, the program accesses the value for key "a" using val, ok := m["a"]. Since "a" exists, val is 1 and ok is true. The program prints these values. Next, it tries to access a missing key "c". The result is val2 = 0 (zero value for int) and ok2 = false, indicating the key is missing. This pattern helps safely check if a key exists before using its value. Variables are tracked step-by-step to show their changes. Key moments clarify why missing keys return zero values and the meaning of the ok variable. The quiz tests understanding of these steps. This is a beginner-friendly way to learn map access in Go.