Menu
Coddy logo textTech

Creating Maps with `make`

Part of the Fundamentals section of Coddy's GO journey. Lesson 86 of 109.

The make function provides another way to create maps, especially when you don't need to initialize values immediately.

Create an empty map with make:

scores := make(map[string]int)
fmt.Println(scores)

Result:

map[]

Add elements to the map after creation:

scores["Alice"] = 95
scores["Bob"] = 80
fmt.Println(scores)

Result:

map[Alice:95 Bob:80]
challenge icon

Challenge

Beginner

In this challenge, you'll practice creating a map using the make function in Go. You'll create a map that stores the favorite colors of different people.

Your task is to create a map using make and then add some key-value pairs to it.

Try it yourself

package main

import "fmt"

func main() {
	// TODO: Create a map called 'favoriteColors' using make
	// The keys should be strings (names) and the values should also be strings (colors)
	
	// Adding some key-value pairs to the map
	favoriteColors["Alice"] = "Blue"
	favoriteColors["Bob"] = "Green"
	favoriteColors["Charlie"] = "Red"
	
	// Print the map
	fmt.Println("Favorite colors:", favoriteColors)
	
	// Print Bob's favorite color
	fmt.Println("Bob's favorite color is", favoriteColors["Bob"])
}
quiz iconTest yourself

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

All lessons in Fundamentals

Practice on your own: Online Go compiler