Hello there! In this lesson, we will apply maps to real-world challenges. Our focus will be on solving tasks such as cataloging books in a library, counting votes in an election, and tracking inventories.
Maps are beneficial in real-life applications, such as the ones mentioned above, due to their ability to rapidly retrieve data with unique keys and efficiently handle larger datasets. Let's understand their efficiency with some actual examples.
Suppose you're asked to manage the cataloging of books in a library. Here, the book ID
serves as the key, while the details of the book, such as the title, author, and year of publication, are stored as values.
This approach allows us to add, search for, and remove books from our library catalog using Go's maps.
Go1package main 2 3import "fmt" 4 5func main() { 6 // Initializing a map with nested maps 7 libraryCatalog := make(map[string]map[string]string) 8 9 // Details of a book 10 bookId := "123" 11 // Creating a map to store details of the book 12 bookDetails := map[string]string{ 13 "title": "To Kill a Mockingbird", 14 "author": "Harper Lee", 15 "year_published": "1960", 16 } 17 18 libraryCatalog[bookId] = bookDetails // Adding a book to library catalog 19 20 // Searching for a book 21 if details, ok := libraryCatalog[bookId]; ok { 22 fmt.Printf("Title: %s, Author: %s, Year Published: %s\n", 23 details["title"], details["author"], details["year_published"]) 24 } 25 26 // Removing a book from the library 27 delete(libraryCatalog, bookId) 28}
make()
is a built-in Go function used to initialize maps, slices, and channels. For example, libraryCatalog := make(map[string]map[string]string)
initializes an empty map where string
keys map to nested maps, ready for storing key-value pairs.
As you can see, maps make the task of cataloging books in the library simpler and more efficient!
Imagine a scenario in which we need to count votes in an election. We employ a map
, where each name is a unique key, and the frequency of that name serves as the associated value. Let's write some Go code to better understand this.
Go1package main 2 3import "fmt" 4 5func main() { 6 // Cast votes 7 votesList := []string{"Alice", "Bob", "Alice", "Charlie", "Bob", "Alice"} 8 9 // Initializing a map 10 voteCounts := make(map[string]int) 11 12 // Counting the votes 13 for _, name := range votesList { 14 voteCounts[name]++ 15 } 16 17 for name, count := range voteCounts { 18 fmt.Printf("%s: %d\n", name, count) 19 } 20 // Prints: Alice: 3, Bob: 2, Charlie: 1 21}
Maps facilitate the efficient counting of votes.
Finally, consider a task that involves managing a store's inventory. Here, we can use a map
in which product names are keys, and quantities are values. This approach allows us to easily add new items, adjust the quantity of items, check whether an item is in stock, and much more.
Go1package main 2 3import "fmt" 4 5func main() { 6 // Initializing an inventory 7 storeInventory := map[string]int{ 8 "Apples": 100, 9 "Bananas": 80, 10 "Oranges": 90, 11 } 12 13 // Adding a new product to inventory and setting its quantity 14 storeInventory["Pears"] = 50 15 16 // Updating the number of apples in inventory 17 if _, ok := storeInventory["Apples"]; ok { 18 storeInventory["Apples"] += 20 19 } 20 21 // A product to be checked 22 prod := "Apples" 23 fmt.Printf("Total %s in stock: %d\n", prod, storeInventory[prod]) 24 25 // Check if a product is in stock 26 prod = "Mangoes" 27 if _, ok := storeInventory[prod]; ok { 28 fmt.Printf("%s are in stock.\n", prod) // If mangoes exist in inventory 29 } else { 30 fmt.Printf("%s are out of stock.\n", prod) // If mangoes don't exist in inventory 31 } 32}
Thus, when managing inventory data, maps offer an efficient solution!
In this lesson, we bridged the gap between the theory of maps and their practical applications. We explored real-world problems that can be solved using maps and implemented Go code to address them.
Now, get ready for hands-on practice exercises that will help reinforce these concepts and hone your map problem-solving skills. Happy coding!