📖 Scenario: You have a map of fruits and their quantities in a store. Sometimes, fruits run out of stock and need to be removed from the map.
🎯 Goal: Learn how to delete entries from a Go map when the quantity is zero.
📋 What You'll Learn
Create a map called inventory with exact fruit names and quantities
Create a variable called threshold set to 0
Use a for loop with variables fruit and quantity to iterate over inventory
Delete entries from inventory where quantity is equal to threshold
Print the final inventory map
💡 Why This Matters
🌍 Real World
Managing inventory data where items with zero stock need to be removed to keep the data clean.
💼 Career
Understanding how to manipulate maps and remove entries is important for backend development, data processing, and managing state in Go applications.
Progress0 / 4 steps
1
Create the initial inventory map
Create a map called inventory with these exact entries: "apple": 5, "banana": 0, "orange": 3, "grape": 0
Go
Hint
Use map[string]int to create a map with string keys and int values.
2
Add a threshold variable
Create an integer variable called threshold and set it to 0
Go
Hint
Use threshold := 0 to create the variable.
3
Delete entries with quantity equal to threshold
Use a for loop with variables fruit and quantity to iterate over inventory. Inside the loop, delete the entry from inventory if quantity is equal to threshold
Go
Hint
Use delete(map, key) to remove an entry from a map.
4
Print the updated inventory
Write fmt.Println(inventory) to display the updated inventory map
Go
Hint
Use fmt.Println(inventory) to print the map.
Practice
(1/5)
1. What does the built-in delete function do in Go when used as delete(myMap, key)?
easy
A. Adds a new entry with the specified key to the map.
B. Returns the value associated with the key.
C. Clears all entries from the map.
D. Removes the entry with the specified key from the map if it exists.
Solution
Step 1: Understand the delete function purpose
The delete function is used to remove a key and its value from a map.
Step 2: Analyze the effect of delete on the map
When called as delete(myMap, key), it removes the entry if the key exists; otherwise, it does nothing.
Final Answer:
Removes the entry with the specified key from the map if it exists. -> Option D
var m map[string]int
delete(m, "key")
fmt.Println("Done")
What will happen when this code runs?
medium
A. Compilation error because delete cannot be used on nil maps.
B. Runtime panic due to deleting from a nil map.
C. Prints "Done" without error.
D. Deletes the key if it exists, then prints "Done".
Solution
Step 1: Understand nil map behavior
In Go, a nil map behaves like an empty map for reads and deletes; deleting a key from a nil map does nothing and does not panic.
Step 2: Analyze the code behavior
Deleting from a nil map is safe and does not cause runtime panic. The program prints "Done" normally.
Final Answer:
Prints "Done" without error. -> Option C
Quick Check:
Deleting from nil map is safe [OK]
Hint: Deleting from nil map is safe, no panic occurs [OK]
Common Mistakes:
Assuming runtime panic on deleting from nil map
Thinking delete causes compilation error
Confusing nil map with uninitialized map variable
5. You have a map m := map[string]int{"x": 10, "y": 0, "z": -5}. You want to delete all keys whose values are zero or less. Which code snippet correctly deletes these entries?
hard
A. for k, v := range m { if v <= 0 { delete(m, k) } }
B. for k := range m { if m[k] < 0 { delete(m, k) } }
C. for k, v := range m { if v == 0 { delete(m, k) } }
D. for k := range m { if m[k] == 0 { delete(m, k) } }
Solution
Step 1: Identify keys with values zero or less
Keys with values <= 0 are "y" (0) and "z" (-5).
Step 2: Choose loop that deletes keys with v <= 0
for k, v := range m { if v <= 0 { delete(m, k) } } uses for k, v := range m and deletes keys where v <= 0, correctly removing "y" and "z".
Step 3: Compare other options
Options A and C check only for == 0, missing negative values like "z":-5. for k := range m { if m[k] < 0 { delete(m, k) } } checks m[k] < 0, missing zero values like "y":0.
Final Answer:
for k, v := range m { if v <= 0 { delete(m, k) } } -> Option A
Quick Check:
Delete keys with value <= 0 using k,v loop [OK]
Hint: Use for k,v range and delete if v <= 0 [OK]
Common Mistakes:
Deleting only keys with value == 0, missing negatives
Using only key without value in loop
Modifying map incorrectly during iteration
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.