Complete the code to delete the key "apple" from the map.
m := map[string]int{"apple": 5, "banana": 3}
delete(m, [1])The delete function removes the specified key from the map. Here, we delete the key "apple".
Complete the code to delete the key stored in variable key from the map.
m := map[string]int{"cat": 1, "dog": 2}
key := "dog"
delete(m, [1])To delete a key stored in a variable, pass the variable name to delete. Here, key holds "dog".
Fix the error in the code to delete the key "blue" from the map.
colors := map[string]string{"red": "#FF0000", "blue": "#0000FF"}
delete([1], "blue")The delete function requires the map variable as the first argument. Here, the map is colors.
Fill both blanks to delete keys from the map only if they exist.
m := map[string]int{"x": 10, "y": 20}
if _, ok := m[[1]]; ok {
delete(m, [2])
}We check if key "x" exists, then delete it. Both blanks use the same key "x".
Fill all three blanks to delete keys from the map only if their values are zero.
m := map[string]int{"a": 0, "b": 1, "c": 0}
for k, v := range m {
if v == [1] {
delete([2], [3])
}
}We delete keys whose value is zero. So, compare v == 0, delete from map m, key k.