Adding and Updating Key-Value
Part of the Fundamentals section of Coddy's GO journey — lesson 87 of 109.
To add or update values in a map, use the assignment operator with the key in square brackets.
Create a map of student scores:
students := make(map[string]int)
// Add new key-value pairs
students["Emma"] = 92
students["Jack"] = 78
fmt.Println(students)Result:
map[Emma:92 Jack:78]Update an existing value:
students["Jack"] = 85
fmt.Println(students)Result:
map[Emma:92 Jack:85]Challenge
BeginnerIn this challenge, you'll practice adding and updating values in a map. We have a map that stores the favorite fruits of different people. Your task is to add a new person's favorite fruit and update an existing person's favorite fruit.
Cheat sheet
To add or update values in a map, use the assignment operator with the key in square brackets:
students := make(map[string]int)
// Add new key-value pairs
students["Emma"] = 92
students["Jack"] = 78
// Update an existing value
students["Jack"] = 85Try it yourself
package main
import (
"fmt"
"sort"
)
func main() {
// Map of people and their favorite fruits
favoriteFruits := map[string]string{
"Alice": "Apple",
"Bob": "Banana",
"Carol": "Cherry",
}
// TODO: Add a new entry for "David" with favorite fruit "Dragon Fruit"
// TODO: Update Bob's favorite fruit to "Blueberry"
// Collect the names (keys) into a slice to sort them
var names []string
for person := range favoriteFruits {
names = append(names, person)
}
sort.Strings(names)
// Print the updated map in sorted order
fmt.Println("Updated favorite fruits:")
for _, person := range names {
fruit := favoriteFruits[person]
fmt.Printf("%s loves %s\n", person, fruit)
}
}
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 Decisions9Pointers
What is a Pointer?Declaring Pointer VariablesThe Address-Of OperatorDereferencing PointersUsing Pointers in FunctionsNil PointersRecap - Understanding Pointers12Composite Types: Maps
Introduction to MapsDeclaring Map LiteralsCreating Maps with `make`Adding and Updating Key-ValueAccessing Map ValuesChecking for Key ExistenceDeleting Map EntriesMap LengthIterating Over MapsRecap - Key-Value Storage