0
0
Goprogramming~30 mins

Output formatting basics in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Output formatting basics
๐Ÿ“– Scenario: You are working in a small shop and want to print a receipt with prices and totals neatly aligned.
๐ŸŽฏ Goal: Learn how to format output in Go to show prices with two decimal places and align text for a clear receipt.
๐Ÿ“‹ What You'll Learn
Create a map with product names and prices
Create a variable for the total price
Use a for loop to format and print each product and price with two decimals
Print the total price formatted with two decimals
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Shops and restaurants print receipts with prices aligned and formatted for customers.
๐Ÿ’ผ Career
Formatting output is important for creating user-friendly command line tools and reports.
Progress0 / 4 steps
1
Create the product prices map
Create a map called products with these exact entries: "Apple": 0.99, "Banana": 1.29, "Cherry": 2.49
Go
Need a hint?

Use map[string]float64 to create the map with product names as keys and prices as values.

2
Create a total price variable
Create a variable called total of type float64 and set it to 0
Go
Need a hint?

Use total := 0.0 to create a float64 variable initialized to zero.

3
Format and sum product prices
Use a for loop with variables product and price to iterate over products. Inside the loop, add price to total and print product and price formatted with two decimals and aligned in 10 spaces for product and 6 spaces for price.
Go
Need a hint?

Use for product, price := range products to loop. Use fmt.Printf with %-10s for left-aligned product name and %6.2f for price with two decimals.

4
Print the total price formatted
Print the text Total: followed by the total variable formatted with two decimals and aligned in 10 spaces for the label and 6 spaces for the number.
Go
Need a hint?

Use fmt.Printf("%-10s %6.2f\n", "Total:", total) to print the total aligned like the products.