0
0
Goprogramming~20 mins

Map use cases in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Map use cases
📖 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.