Problem overview
The cart has a subtotal of 75, a flat shippingCost of 10 and a freeShippingThreshold of 100. The program prints a three-line summary: the subtotal, the shipping charge and the total. A cart that reaches the threshold ships for free; any other cart pays the flat cost.
With a subtotal of 75 the cart is under the threshold, so shipping is 10 and the total is 85:
Subtotal: $75
Shipping: $10
Total: $85
With a subtotal of 120 the cart ships free, and the shipping line still appears, showing $0. The three lines are always printed, whatever the cart holds. This version adds one decision to the earlier receipt: should shipping be charged at all?
Approach: decide shipping first, then build the lines
The total depends on the shipping, and the shipping depends on a comparison. So the program works in that order. First it decides the shipping amount with an if/else. Then it adds the shipping to the subtotal. Last it turns the numbers into text and prints them.
The comparison uses >=, not >. A cart of exactly 100 meets the threshold, so it ships free.
An if/else fits because exactly one of two things happens. The two branches can never both run, and one of them always does. Two separate if statements, one for each case, would also work, but then the second condition has to repeat the first one in reverse, and a mistake in either leaves a cart with no shipping value at all.
Another thing to notice is that the program never changes subtotal, shippingCost or freeShippingThreshold. It reads them. That makes it easy to try other carts: change the 75 and run it again. The rules stay the same for any starting value.
Step by step
- Declare
shippingbefore theif. A variable created inside a block disappears when the block ends. The total and the lines are built after theif/else, soshippingmust live outside it. Writingvar shipping intcreates it with a type and no value yet (Go fills in0). - Open an
ifonsubtotal >= freeShippingThreshold. Go needs no parentheses around the condition, but the braces are required. - Set
shipping = 0in theifblock. This is the free-shipping case. It uses=, not:=, because the variable already exists. With:=the block would create a new, separateshippingthat vanishes at the closing brace. - Add the
elseon the line of the closing brace. Go requires} else {on one line; a line break beforeelseis a syntax error. - Set
shipping = shippingCostin theelseblock. This branch handles every cart under the threshold. - Compute
total. It issubtotal + shipping. When shipping is free, the total equals the subtotal. - Build
line1,line2andline3. Each is a label followed by a number converted withstrconv.Itoa. Go does not join a number to a string with+, so each number has to become text first. - Join the lines into
resultwith"\n"between them: two newlines for three lines. - Print
resultwithfmt.Println. The newlines inside the string put each line on its own row.
The solution in Go
subtotal := 75
shippingCost := 10
freeShippingThreshold := 100
var shipping int
if subtotal >= freeShippingThreshold {
shipping = 0
} else {
shipping = shippingCost
}
total := subtotal + shipping
line1 := "Subtotal: $" + strconv.Itoa(subtotal)
line2 := "Shipping: $" + strconv.Itoa(shipping)
line3 := "Total: $" + strconv.Itoa(total)
result := line1 + "\n" + line2 + "\n" + line3
fmt.Println(result)
Go details worth knowing here:
- Why
strconv.Itoa."Subtotal: $" + subtotaldoes not compile, because+needs both sides to be strings.strconv.Itoa(75)returns"75". The program needs thestrconvandfmtpackages imported. - Branches assign, they don't declare. Both branches use
=on the outershipping. Every path through theif/elsesets it, so it always has the value the later lines need. - Whole numbers only. All amounts are
inthere, so there are no cents. Prices with decimals would need a different type and care with rounding.
Edge cases and common mistakes
- A cart exactly on the threshold. A subtotal of 100 gives
Shipping: $0andTotal: $100. Writing>instead of>=would charge 10 here. - One below the threshold. A subtotal of 99 pays shipping: the total is 109.
- Using
:=inside the branches.shipping := 0inside theifcreates a new variable that hides the outer one. The compiler rejects the program withshipping declared and not used, since the inner variable is never read. - Dropping the shipping line when it is free. The lesson's tests expect all three lines, with
$0on the shipping row. - Forgetting the conversion. Passing an
intto a string join is a compile error, so nothing runs until each number goes throughstrconv.Itoa.
Testing
- Subtotal 75 prints shipping
$10and total$85. - Subtotal 120 prints shipping
$0and total$120. - Subtotal 100 prints shipping
$0and total$100. - Subtotal 99 prints shipping
$10and total$109.
