📖 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
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
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
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
Hint
Use fmt.Printf("%-10s %6.2f\n", "Total:", total) to print the total aligned like the products.
Practice
(1/5)
1. What does the %d format specifier do in fmt.Printf in Go?
easy
A. Prints an integer value
B. Prints a floating-point number
C. Prints a string
D. Prints a boolean value
Solution
Step 1: Understand format specifiers in Go
The %d specifier is used to print decimal integers in Go's fmt.Printf.
Step 2: Match specifier to data type
Since %d prints integers, it matches integer values, not floats, strings, or booleans.
Final Answer:
Prints an integer value -> Option A
Quick Check:
%d = integer [OK]
Hint: Remember %d is for decimal integers in printf [OK]
Common Mistakes:
Confusing %d with %f for floats
Using %d for strings
Thinking %d prints booleans
2. Which of the following is the correct syntax to print a string variable name using fmt.Printf in Go?
easy
A. fmt.Printf("%d", name)
B. fmt.Printf("%f", name)
C. fmt.Printf("%s", name)
D. fmt.Printf(name)
Solution
Step 1: Identify the correct format specifier for strings
The format specifier for strings in Go's fmt.Printf is %s.
Step 2: Check the syntax correctness
fmt.Printf("%s", name) uses fmt.Printf("%s", name), which correctly prints the string variable name.
Final Answer:
fmt.Printf("%s", name) -> Option C
Quick Check:
%s prints strings [OK]
Hint: Use %s for strings in fmt.Printf [OK]
Common Mistakes:
Using %d or %f for strings
Omitting quotes around format string
Passing variable without format specifier
3. What is the output of the following Go code?
package main
import "fmt"
func main() {
x := 7
y := 3.14
fmt.Printf("x = %d, y = %.1f\n", x, y)
}
medium
A. x = 7, y = 3.1
B. x = 7, y = 3.14
C. x = 7, y = 3.0
D. x = 7, y = 3
Solution
Step 1: Understand the format specifiers used
%d prints integer x as 7. %.1f prints floating-point y rounded to 1 decimal place.
Step 2: Apply rounding to y
3.14 rounded to 1 decimal place is 3.1, so output is "x = 7, y = 3.1".
Final Answer:
x = 7, y = 3.1 -> Option A
Quick Check:
%.1f rounds float to 1 decimal [OK]
Hint: %.1f rounds float to 1 decimal place [OK]
Common Mistakes:
Expecting full float without rounding
Confusing %d with %f
Ignoring newline character
4. Identify the error in this Go code snippet:
package main
import "fmt"
func main() {
name := "Alice"
age := 30
fmt.Printf("Name: %s, Age: %f\n", name, age)
}
medium
A. Missing newline character in format string
B. Missing import statement
C. Incorrect variable declaration
D. Using %f to print an integer variable
Solution
Step 1: Check format specifiers against variable types
name is a string, printed with %s correctly. age is an integer but printed with %f, which expects a float.
Step 2: Identify the mismatch causing error
Using %f for an integer causes a runtime error or incorrect output.
Final Answer:
Using %f to print an integer variable -> Option D
Quick Check:
Match %f with float, not int [OK]
Hint: Match %f only with float variables [OK]
Common Mistakes:
Using %f for integers
Ignoring type mismatch warnings
Assuming %f works for all numbers
5. You want to print a table of product names and prices aligned in columns using fmt.Printf. Which format string correctly aligns the name left in 10 spaces and price right in 6 spaces with 2 decimals?
fmt.Printf("...", name, price)
hard
A. "%10s %-6.2f\n"
B. "%-10s %6.2f\n"
C. "%10s %6.2f\n"
D. "%-10s %-6.2f\n"
Solution
Step 1: Understand alignment flags in format strings
In Go, %-10s means left-align string in 10 spaces; %6.2f means right-align float in 6 spaces with 2 decimals.
Step 2: Match alignment to requirements
We want product name left-aligned in 10 spaces and price right-aligned in 6 spaces with 2 decimals, so "%-10s %6.2f\n" fits.
Final Answer:
"%-10s %6.2f\n" -> Option B
Quick Check:
Left-align string with %-10s, right-align float with %6.2f [OK]
Hint: Use - for left-align strings, no - for right-align numbers [OK]
Common Mistakes:
Swapping alignment flags
Using wrong width or precision
Forgetting to specify decimal places for floats
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.