Deleting Map Entries
Part of the Fundamentals section of Coddy's GO journey — lesson 90 of 109.
To delete an entry from a map in Go, use the built-in delete function. This removes a key-value pair from the map.
Create a map of scores:
scores := map[string]int{
"Alice": 95,
"Bob": 82,
"Charlie": 78,
}
fmt.Println(scores)Delete Bob's score from the map:
delete(scores, "Bob")
fmt.Println(scores)Result:
map[Alice:95 Charlie:78]Deleting a key that doesn't exist is safe and has no effect:
delete(scores, "David") // No errorChallenge
BeginnerIn this challenge, you'll practice deleting entries from a map in Go.
We have a map called inventory that contains items and their quantities. Your task is to delete the item 'pencil' from the inventory map using the delete function.
After deleting the entry, the program will check if the item still exists in the map and print the results.
Cheat sheet
To delete an entry from a map in Go, use the built-in delete function:
delete(mapName, key)Example:
scores := map[string]int{
"Alice": 95,
"Bob": 82,
"Charlie": 78,
}
delete(scores, "Bob")
// Result: map[Alice:95 Charlie:78]Deleting a key that doesn't exist is safe and has no effect:
delete(scores, "David") // No errorTry it yourself
package main
import "fmt"
func main() {
// Map of inventory items and their quantities
inventory := map[string]int{
"pen": 15,
"pencil": 10,
"paper": 25,
"eraser": 5,
}
fmt.Println("Initial inventory:", inventory)
// TODO: Delete the 'pencil' entry from the inventory map
fmt.Println("Updated inventory:", inventory)
// Check if 'pencil' still exists in the map
_, exists := inventory["pencil"]
fmt.Println("Does 'pencil' exist in inventory?", exists)
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions