Maps store pairs of keys and values. Accessing map values lets you find information quickly using a key.
You want to find a phone number by a person's name.
You need to get a product price using its ID.
You want to check if a username exists in a list.
You want to count how many times a word appears in a text.
Syntax
Go
value, ok := mapName[key]
value gets the data stored for key.
ok is a boolean that is true if the key exists, false if not.
Examples
Get the value for key "Alice" without checking if it exists.
Go
age := ages["Alice"]
Get value and check if "Bob" exists before using it.
Go
age, ok := ages["Bob"]
if ok {
fmt.Println("Bob's age is", age)
} else {
fmt.Println("Bob not found")
}
Sample Program
This program shows how to get values from a map. It prints Alice's age and checks if Charlie is in the map before printing.
Go
package main
import"fmt"
func main() {
ages := map[string]int{"Alice": 30, "Bob": 25}
// Access value directly
aliceAge := ages["Alice"]
fmt.Println("Alice's age:", aliceAge)
// Access with check
charlieAge, ok := ages["Charlie"]
if ok {
fmt.Println("Charlie's age:", charlieAge)
} else {
fmt.Println("Charlie not found in map")
}
}
OutputSuccess
Important Notes
If you access a key that does not exist without checking, Go returns the zero value for the value type (like 0 for int).
Always check the boolean ok if you need to know whether the key was present.
Summary
Maps store key-value pairs for quick lookup.
Use value, ok := map[key] to get a value and check if the key exists.
Without checking, missing keys return zero values.
Practice
(1/5)
1. What does the expression value, ok := myMap["key"] do in Go?
easy
A. It retrieves the value for "key" and checks if the key exists in the map.
B. It deletes the key "key" from the map.
C. It adds a new key "key" with value "ok" to the map.
D. It returns the number of keys in the map.
Solution
Step 1: Understand map access syntax
In Go, value, ok := myMap["key"] retrieves the value for the key and a boolean indicating if the key exists.
Step 2: Explain the boolean ok
The ok is true if the key is present, false otherwise.
Final Answer:
It retrieves the value for "key" and checks if the key exists in the map. -> Option A
Quick Check:
Map access with existence check = value, ok [OK]
Hint: Remember: value, ok checks key presence in map [OK]
Common Mistakes:
Thinking it deletes the key
Assuming it adds a key
Confusing it with map length
2. Which of the following is the correct way to check if a key exists in a Go map named m for key k?
easy
A. value, ok := m[k]; if ok { ... }
B. value := m[k]; if value != nil { ... }
C. if m.contains(k) { ... }
D. if m[k] != 0 { ... }
Solution
Step 1: Recall Go map existence check syntax
In Go, to check if a key exists, use value, ok := m[k] and then check ok.
Step 2: Analyze options
value := m[k]; if value != nil { ... } wrongly assumes value is nil for missing keys, which is not always true. if m.contains(k) { ... } uses a method that does not exist. if m[k] != 0 { ... } checks value but zero value can be valid.
Final Answer:
value, ok := m[k]; if ok { ... } -> Option A
Quick Check:
Use value, ok to check key presence [OK]
Hint: Use value, ok := map[key] to check key existence [OK]
Common Mistakes:
Checking value against nil or zero instead of ok
Using non-existent map methods
Assuming zero value means key missing
3. What will be the output of this Go code?
m := map[string]int{"a": 10, "b": 20}
value, ok := m["c"]
fmt.Println(value, ok)
medium
A. Compilation error
B. 0 true
C. 20 true
D. 0 false
Solution
Step 1: Understand map lookup for missing key
Key "c" is not in map m, so value gets zero value for int, which is 0, and ok is false.
Step 2: Print output values
Printing value and ok prints "0 false".
Final Answer:
0 false -> Option D
Quick Check:
Missing key returns zero value and false [OK]
Hint: Missing map keys return zero value and false [OK]
Common Mistakes:
Assuming missing key returns true
Expecting runtime error for missing key
Confusing zero value with actual stored value
4. Identify the error in this Go code snippet:
m := map[string]int{"x": 5}
value := m["y"]
if value {
fmt.Println("Key exists")
}
medium
A. Missing declaration of map m.
B. Map key "y" does not exist, causing runtime panic.
C. Cannot use int value as boolean in if condition.
D. Syntax error in map initialization.
Solution
Step 1: Analyze the if condition
The variable value is an int, but Go requires a boolean expression in if conditions.
Step 2: Understand Go type rules
Using an int directly as a boolean causes a compile-time error.
Final Answer:
Cannot use int value as boolean in if condition. -> Option C
Quick Check:
if conditions need boolean, not int [OK]
Hint: if needs boolean, not int value [OK]
Common Mistakes:
Assuming missing key causes panic
Ignoring type mismatch in if condition
Thinking map declaration is missing
5. Given a map m := map[string]int{"a": 1, "b": 0, "c": 3}, how can you print only keys that exist and have a non-zero value?
hard
A. for k := range m { if m[k] { fmt.Println(k) } }
B. for k, v := range m { if v != 0 { fmt.Println(k) } }
C. for k, v := range m { if m[k] != nil { fmt.Println(k) } }
D. for k, v := range m { if v { fmt.Println(k) } }
Solution
Step 1: Iterate over map with key and value
Use for k, v := range m to get keys and values.
Step 2: Check for non-zero values
Check if v != 0 to filter keys with non-zero values.
Step 3: Print keys that meet condition
Print the key k inside the if block.
Final Answer:
for k, v := range m { if v != 0 { fmt.Println(k) } } -> Option B
Quick Check:
Filter keys by value != 0 using range loop [OK]
Hint: Use for k,v := range map and check v != 0 [OK]
Common Mistakes:
Using value as boolean directly
Checking map key against nil
Ignoring zero values in condition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.