📖 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.
Practice
(1/5)
1. Why are maps used in Go programming?
easy
A. To create graphical user interfaces
B. To perform mathematical calculations
C. To store data with keys for fast lookup
D. To handle file input and output
Solution
Step 1: Understand the purpose of maps
Maps store data in key-value pairs, allowing quick access to values using keys.
Step 2: Compare with other uses
Maps are not used for calculations, GUI, or file handling, which are handled by other packages or types.
Final Answer:
To store data with keys for fast lookup -> Option C
Quick Check:
Maps = Fast key-based data access [OK]
Hint: Maps = key-value pairs for quick data access [OK]
Common Mistakes:
Confusing maps with slices or arrays
Thinking maps are for calculations
Assuming maps handle files or UI
2. Which of the following is the correct way to create a map in Go?
easy
A. var m = map[string]int{1, 2, 3}
B. var m map[string]int = make(map[string]int)
C. var m = []int{1, 2, 3}
D. var m = make([]string, 5)
Solution
Step 1: Identify correct map creation syntax
Maps are created using make with the syntax: make(map[keyType]valueType).
Step 2: Check each option
var m map[string]int = make(map[string]int) uses make with map[string]int correctly. Options B and D create slices, and A has wrong map literal syntax.
Final Answer:
var m map[string]int = make(map[string]int) -> Option B
Quick Check:
Use make(map[key]value) to create maps [OK]
Hint: Use make(map[keyType]valueType) to create maps [OK]
Common Mistakes:
Using slice syntax instead of map
Forgetting to use make before adding items
Incorrect map literal syntax
3. What will be the output of the following Go code?