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 and is used to access its corresponding value.
Click to reveal answer
beginner
How do you access a value from a map in Go?
You access a value by using the key inside square brackets after the map variable, like: value := myMap[key].
Click to reveal answer
beginner
What happens if you try to access a key that does not exist in a Go map?
Go returns the zero value of the map's value type (e.g., 0 for int, "" for string) if the key is not found.
Click to reveal answer
intermediate
How can you check if a key exists in a Go map when accessing its value?
You can use the two-value assignment: value, ok := myMap[key]. The 'ok' is true if the key exists, false otherwise.
Click to reveal answer
intermediate
Why is it useful to check if a key exists in a Go map before using its value?
Because accessing a non-existent key returns a zero value, which might be misleading. Checking existence helps avoid bugs by confirming the key is present.
Click to reveal answer
How do you access the value for key "name" in a Go map called 'person'?
✗ Incorrect
In Go, map values are accessed using square brackets with the key inside, like person["name"].
What does Go return if you access a key that does not exist in a map?
✗ Incorrect
Go returns the zero value for the map's value type if the key is missing, without throwing an error.
Which syntax correctly checks if a key exists in a Go map?
✗ Incorrect
The two-value assignment 'value, ok := myMap[key]' lets you check if the key exists with 'ok'.
What type is the variable 'ok' when checking map key existence in Go?
✗ Incorrect
'ok' is a boolean that is true if the key exists, false otherwise.
Why might you want to check if a key exists before using its value from a map?
✗ Incorrect
Checking existence helps avoid confusion caused by zero values returned for missing keys.
Explain how to access a value from a map in Go and how to check if the key exists.
Think about how you get a value and also know if it is really there.
You got /3 concepts.
Describe what happens when you access a key that does not exist in a Go map and why checking existence is important.
Consider what Go returns and how that might confuse your program.
You got /3 concepts.