📖 Scenario: You are managing a small library system. You want to keep track of books and their availability status using a map in Go.
🎯 Goal: Build a Go program that uses a map to store book titles as keys and their availability (true for available, false for checked out) as values. Then, filter and display only the available books.
📋 What You'll Learn
Create a map called library with exact book titles as keys and boolean values for availability
Create a variable called availableBooks to store available books
Use a for loop with variables title and available to iterate over library
Add available book titles to availableBooks
Print the availableBooks slice
💡 Why This Matters
🌍 Real World
Maps are useful to store and quickly access data by keys, like book titles in a library system.
💼 Career
Understanding maps and slices is essential for backend development, data processing, and building efficient Go applications.
Progress0 / 4 steps
1
Create the library map
Create a map called library with these exact entries: "The Hobbit": true, "1984": false, "Pride and Prejudice": true, "The Catcher in the Rye": false
Go
Hint
Use map[string]bool to create the map with book titles as keys and availability as boolean values.
2
Create the availableBooks slice
Create a variable called availableBooks as an empty slice of strings to store titles of available books
Go
Hint
Use []string{} to create an empty slice of strings.
3
Filter available books
Use a for loop with variables title and available to iterate over library. Inside the loop, if available is true, append title to availableBooks
Go
Hint
Use for title, available := range library to loop over the map. Use append to add to the slice.
4
Print available books
Write fmt.Println(availableBooks) to display the list of available books
Go
Hint
Use fmt.Println(availableBooks) to print the slice.
Practice
(1/5)
1. What is the primary use of a map in Go?
easy
A. To store data with keys for fast lookup
B. To perform mathematical calculations
C. To create user interfaces
D. To handle file input/output
Solution
Step 1: Understand the purpose of a map
A map in Go is a collection of key-value pairs, designed for fast data retrieval using keys.
Step 2: Compare with other options
Options B, C, and D describe unrelated tasks like calculations, UI, or file handling, which are not the main use of maps.
Final Answer:
To store data with keys for fast lookup -> Option A
Quick Check:
Maps = key-value storage [OK]
Hint: Maps store key-value pairs for quick access [OK]
Common Mistakes:
Confusing maps with slices or arrays
Thinking maps are for calculations
Assuming maps handle UI or files
2. Which of the following is the correct way to create an empty map in Go?
easy
A. m := make([]string, 0)
B. var m map[string]int = make(map[string]int)
C. m := map[string]int
D. m := []int{}
Solution
Step 1: Identify map creation syntax
In Go, maps are created using make(map[keyType]valueType). var m map[string]int = make(map[string]int) uses this correctly.
Step 2: Check other options
m := []int{} creates a slice, not a map. m := map[string]int is invalid syntax for map creation. m := make([]string, 0) creates a slice, not a map.
Final Answer:
var m map[string]int = make(map[string]int) -> Option B