Recall & Review
beginner
What is a map in Go?
A map in Go is a collection of key-value pairs where each key is unique. It is like a dictionary or a phone book where you look up a value using a key.
Click to reveal answer
beginner
How do you start iterating over a map in Go?
You use a
for loop with the range keyword. It lets you access each key and value in the map one by one.Click to reveal answer
beginner
What does the
range keyword return when used on a map?It returns two values: the key and the value for each item in the map during the loop.
Click to reveal answer
intermediate
Can you control the order of iteration over a map in Go?
No, the order of iteration over a map is random and can change each time you run the program. Maps do not keep items in order.
Click to reveal answer
intermediate
How do you ignore the value or the key when iterating over a map?
Use the blank identifier
_ to ignore either the key or the value. For example, for k := range m ignores the value, and for _, v := range m ignores the key.Click to reveal answer
What does the
range keyword return when used in a for loop over a map?✗ Incorrect
When iterating over a map, range returns both the key and the value for each element.
How can you ignore the key when iterating over a map?
✗ Incorrect
Using the blank identifier _ for the key lets you ignore it and only use the value.
Is the order of iteration over a Go map guaranteed to be the same every time?
✗ Incorrect
Maps in Go do not keep order, so iteration order is random and can change each run.
Which of these is the correct syntax to iterate over a map named
m?✗ Incorrect
The for k, v := range m syntax is the correct way to loop over a map in Go.
What happens if you use
for k := range m?✗ Incorrect
This syntax returns only the keys, ignoring the values.
Explain how to iterate over a map in Go and how to access keys and values inside the loop.
Think about how you use a for loop with range to get both key and value.
You got /3 concepts.
Describe why the order of iteration over a map in Go is not predictable and what that means for your code.
Consider how a phone book might be shuffled each time you look at it.
You got /3 concepts.