0
0
Goprogramming~15 mins

Operator precedence in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Operator precedence
๐Ÿ“– Scenario: Imagine you are calculating the total cost of items in a shopping cart with discounts and taxes. You need to understand how Go handles operator precedence to get the correct total.
๐ŸŽฏ Goal: Build a simple Go program that calculates the final price of an item after applying a discount and adding tax, using operator precedence correctly.
๐Ÿ“‹ What You'll Learn
Create variables for price, discount, and tax rate with exact values
Create a variable for discounted price using subtraction and multiplication
Calculate the final price by adding tax to the discounted price using correct operator precedence
Print the final price
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Calculating prices with discounts and taxes is common in shopping and billing systems.
๐Ÿ’ผ Career
Understanding operator precedence helps avoid bugs in financial calculations and data processing.
Progress0 / 4 steps
1
Create initial price variables
Create three variables in Go: price with value 100.0, discount with value 0.2, and taxRate with value 0.1. Use type float64 for all.
Go
Need a hint?

Use var keyword to declare variables with type float64.

2
Calculate discounted price
Add a variable discountedPrice that calculates the price after discount using the formula price - price * discount.
Go
Need a hint?

Remember multiplication happens before subtraction.

3
Calculate final price with tax
Create a variable finalPrice that adds tax to the discounted price using the formula discountedPrice + discountedPrice * taxRate.
Go
Need a hint?

Multiplication happens before addition, so no parentheses needed.

4
Print the final price
Use fmt.Printf to print the finalPrice with two decimal places, using the format string "Final price: %.2f\n".
Go
Need a hint?

Use fmt.Printf with "%.2f" to format the float with two decimals.