0
0
Goprogramming~15 mins

Deleting map entries in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Deleting map entries
📖 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.