Menu
Coddy logo textTech

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 icon

Challenge

Beginner

In 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"] = 85

Try 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)
	}
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals