📖 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
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
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
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
Hint
Use fmt.Printf with "%.2f" to format the float with two decimals.
Practice
(1/5)
1. In Go, which operator has the highest precedence among these: +, -, *, /?
easy
A. Subtraction (-)
B. Addition (+)
C. All have the same precedence
D. Multiplication (*) and division (/)
Solution
Step 1: Recall operator precedence rules in Go
Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-).
Step 2: Compare given operators
Among the given operators, * and / are evaluated before + and -.
Final Answer:
Multiplication (*) and division (/) -> Option D
Quick Check:
*, / > +, - [OK]
Hint: Remember: multiply/divide before add/subtract [OK]
Common Mistakes:
Thinking all operators have equal precedence
Confusing addition with multiplication precedence
Ignoring division operator precedence
2. Which of the following Go expressions is syntactically correct and respects operator precedence?
easy
A. result := 5 + (3 * 2
B. result := 5 + * 3 2
C. result := 5 + 3 * 2
D. result := 5 + 3 * * 2
Solution
Step 1: Check each expression for syntax correctness
result := 5 + 3 * 2 uses valid operators and correct syntax. Options B, C, and D have syntax errors like misplaced operators or missing parentheses.
Step 2: Confirm operator precedence in result := 5 + 3 * 2
Multiplication (*) happens before addition (+), so expression is valid and respects precedence.
Final Answer:
result := 5 + 3 * 2 -> Option C
Quick Check:
Valid syntax and precedence in result := 5 + 3 * 2 [OK]
Hint: Check for misplaced or missing operators and parentheses [OK]
Common Mistakes:
Using two operators in a row without operand
Missing closing parentheses
Confusing operator placement
3. What is the output of this Go code?
package main
import "fmt"
func main() {
x := 2 + 3 * 4 - 6 / 2
fmt.Println(x)
}