0
0
Goprogramming~30 mins

Best practices in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Best practices in Go programming
📖 Scenario: You are working on a small Go program that calculates the total price of items in a shopping cart. You want to write clean, readable, and maintainable code following Go best practices.
🎯 Goal: Build a Go program that defines a map of items with their prices, calculates the total price using a loop, and prints the result. You will apply best practices such as clear variable naming, proper indentation, and concise code.
📋 What You'll Learn
Create a map named items with string keys and float64 values
Create a variable named total to hold the sum of prices
Use a for loop with variables item and price to iterate over items
Add each price to total
Print the total price with a clear message
💡 Why This Matters
🌍 Real World
Calculating totals from a list of products and prices is common in shopping cart applications and billing systems.
💼 Career
Understanding maps, loops, and formatted output is essential for Go developers working on backend services, e-commerce platforms, and data processing.
Progress0 / 4 steps
1
Create the items map
Create a map called items with these exact entries: "apple": 0.99, "banana": 0.59, "orange": 1.29
Go
Hint

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

2
Add a total variable
Create a variable called total of type float64 and set it to 0
Go
Hint

Use var total float64 = 0 to declare the variable.

3
Calculate the total price
Use a for loop with variables item and price to iterate over items. Add each price to total
Go
Hint

Use for item, price := range items to loop over the map.

4
Print the total price
Write fmt.Printf to print the message Total price: $ followed by the value of total with two decimal places
Go
Hint

Use fmt.Printf("Total price: $%.2f\n", total) to format the output with two decimals.