A map in Go is a built-in data type that stores key-value pairs. It allows you to quickly look up a value by its key, similar to a dictionary in real life where you find a word's meaning by looking it up.
Click to reveal answer
beginner
How do you declare and initialize a map in Go?
You declare a map using the syntax: var m map[string]int. To initialize it, use m = make(map[string]int) or declare and initialize together with m := map[string]int{"key": 1}.
Click to reveal answer
beginner
What happens if you try to access a key that does not exist in a Go map?
If you access a key that does not exist, Go returns the zero value for the map's value type. For example, if the map stores integers, it returns 0. You can also check if the key exists using the two-value assignment: value, ok := m[key].
Click to reveal answer
beginner
Name a common use case for maps in Go.
Maps are commonly used to count occurrences, like counting how many times each word appears in a text. They are also used for fast lookups, caching data, and grouping related information by keys.
Click to reveal answer
beginner
How do you delete a key-value pair from a map in Go?
You use the built-in delete function: delete(m, key). This removes the key and its value from the map.
Click to reveal answer
How do you check if a key exists in a Go map?
Avalue, ok := m[key]
Bexists := m.contains(key)
Cif m[key] != nil
Dm.hasKey(key)
✗ Incorrect
In Go, you use the two-value assignment: value, ok := m[key]. The variable ok is true if the key exists.
What is the zero value returned when accessing a missing key in a map of type map[string]int?
A0
B"" (empty string)
Cnil
Dfalse
✗ Incorrect
For int values, the zero value is 0, so accessing a missing key returns 0.
Which function initializes a map in Go?
Ainit()
Bnew()
Cmake()
Dcreate()
✗ Incorrect
The make() function initializes maps, slices, and channels in Go.
How do you remove a key-value pair from a map?
Am.remove(key)
Bdelete(m, key)
Cm.pop(key)
Dm.clear(key)
✗ Incorrect
The built-in delete(m, key) function removes a key and its value from the map.
Which of these is NOT a typical use case for maps?
ACaching data
BFast data lookup by key
CCounting word frequencies
DStoring ordered lists
✗ Incorrect
Maps do not maintain order, so they are not used for storing ordered lists.
Explain how you would use a map in Go to count how many times each word appears in a sentence.
Think about how a map can store words as keys and counts as values.
You got /5 concepts.
Describe how to safely check if a key exists in a Go map and handle the case when it does not.
Remember the special syntax Go provides for map lookups.
You got /4 concepts.
Practice
(1/5)
1. What is the primary use of a map in Go?
easy
A. To store data with keys for fast lookup
B. To perform mathematical calculations
C. To create user interfaces
D. To handle file input/output
Solution
Step 1: Understand the purpose of a map
A map in Go is a collection of key-value pairs, designed for fast data retrieval using keys.
Step 2: Compare with other options
Options B, C, and D describe unrelated tasks like calculations, UI, or file handling, which are not the main use of maps.
Final Answer:
To store data with keys for fast lookup -> Option A
Quick Check:
Maps = key-value storage [OK]
Hint: Maps store key-value pairs for quick access [OK]
Common Mistakes:
Confusing maps with slices or arrays
Thinking maps are for calculations
Assuming maps handle UI or files
2. Which of the following is the correct way to create an empty map in Go?
easy
A. m := make([]string, 0)
B. var m map[string]int = make(map[string]int)
C. m := map[string]int
D. m := []int{}
Solution
Step 1: Identify map creation syntax
In Go, maps are created using make(map[keyType]valueType). var m map[string]int = make(map[string]int) uses this correctly.
Step 2: Check other options
m := []int{} creates a slice, not a map. m := map[string]int is invalid syntax for map creation. m := make([]string, 0) creates a slice, not a map.
Final Answer:
var m map[string]int = make(map[string]int) -> Option B