0
0
Goprogramming~15 mins

Accessing map values in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Accessing map values
📖 Scenario: You are managing a small store's inventory. You want to check the quantity of certain products quickly.
🎯 Goal: Build a Go program that creates a map of products and their quantities, then accesses and prints the quantity of a specific product.
📋 What You'll Learn
Create a map with product names as keys and quantities as values
Access the quantity of a specific product using its key
Print the quantity to the console
💡 Why This Matters
🌍 Real World
Maps are useful for storing and looking up data quickly, like product inventories or user information.
💼 Career
Understanding maps and how to access their values is essential for many programming tasks in software development.
Progress0 / 4 steps
1
Create the product inventory map
Create a map called inventory with these exact entries: "apple": 10, "banana": 5, "orange": 8.
Go
Hint

Use map[string]int to create a map with string keys and int values.

2
Create a variable for the product to check
Create a variable called product and set it to the string "banana".
Go
Hint

Use product := "banana" to create the variable.

3
Access the quantity of the product
Create a variable called quantity and set it to the value from inventory for the key stored in product.
Go
Hint

Use quantity := inventory[product] to get the value from the map.

4
Print the quantity
Use fmt.Println to print the value of quantity.
Go
Hint

Use fmt.Println(quantity) to print the quantity.