0
0
Goprogramming~15 mins

Why maps are used in Go - See It in Action

Choose your learning style9 modes available
Why maps are used
📖 Scenario: Imagine you run a small library. You want to keep track of how many copies of each book you have. You need a way to quickly find the number of copies by the book's title.
🎯 Goal: You will create a map in Go to store book titles as keys and their copy counts as values. Then you will look up and print the number of copies for a specific book.
📋 What You'll Learn
Create a map with string keys and int values
Add at least three books with their copy counts
Create a variable to hold the title to look up
Use the map to find the copy count for that title
Print the result
💡 Why This Matters
🌍 Real World
Libraries, stores, and many apps use maps to quickly find information by a name or ID.
💼 Career
Understanding maps is important for programming jobs that handle data lookup, caching, and fast access.
Progress0 / 4 steps
1
Create a map of books and their copy counts
Create a map called library with string keys and int values. Add these entries exactly: "Go Programming": 5, "Python Basics": 3, "Java Fundamentals": 4.
Go
Hint

Use map[string]int{} to create the map with string keys and int values.

2
Create a variable for the book title to look up
Create a string variable called bookTitle and set it to "Python Basics".
Go
Hint

Use bookTitle := "Python Basics" to create the variable.

3
Look up the copy count for the book title
Create an int variable called copies and set it to the value from library for the key bookTitle.
Go
Hint

Use copies := library[bookTitle] to get the value from the map.

4
Print the number of copies for the book
Use fmt.Println to print the text "Copies of Python Basics:" followed by the value of copies.
Go
Hint

Use fmt.Println("Copies of Python Basics:", copies) to print the message and value.