0
0
Goprogramming~10 mins

Deleting map entries in Go - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to delete the key "apple" from the map.

Go
m := map[string]int{"apple": 5, "banana": 3}
delete(m, [1])
Drag options to blanks, or click blank then click option'
A"banana"
B"apple"
C"orange"
D"grape"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a key that does not exist in the map.
Forgetting to put the key in quotes.
2fill in blank
medium

Complete the code to delete the key stored in variable key from the map.

Go
m := map[string]int{"cat": 1, "dog": 2}
key := "dog"
delete(m, [1])
Drag options to blanks, or click blank then click option'
Am
B"dog"
C"cat"
Dkey
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the variable name in quotes, which deletes a literal string key.
Passing the whole map instead of the key.
3fill in blank
hard

Fix the error in the code to delete the key "blue" from the map.

Go
colors := map[string]string{"red": "#FF0000", "blue": "#0000FF"}
delete([1], "blue")
Drag options to blanks, or click blank then click option'
Acolor
Bcolors["blue"]
Ccolors
Dcolors[blue]
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a map element instead of the map variable.
Using an undefined variable name.
4fill in blank
hard

Fill both blanks to delete keys from the map only if they exist.

Go
m := map[string]int{"x": 10, "y": 20}
if _, ok := m[[1]]; ok {
    delete(m, [2])
}
Drag options to blanks, or click blank then click option'
A"x"
B"y"
C"z"
D"w"
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys in the check and delete.
Checking a key that does not exist.
5fill in blank
hard

Fill all three blanks to delete keys from the map only if their values are zero.

Go
m := map[string]int{"a": 0, "b": 1, "c": 0}
for k, v := range m {
    if v == [1] {
        delete([2], [3])
    }
}
Drag options to blanks, or click blank then click option'
A1
Bm
Ck
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing to 1 instead of 0.
Deleting from a wrong variable.
Using a string "k" instead of the variable k.