Start learning free

LESSON 191 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V3 in Go: Free Shipping with if/else

Practice Read the full explanation

Use an if/else to decide whether to charge shipping based on a free-shipping threshold.

You start with:

subtotal := 75
shippingCost := 10
freeShippingThreshold := 100

Build:

Subtotal: $75
Shipping: $10
Total: $85

Rules:

  • If subtotal >= freeShippingThreshold, set shipping to 0; otherwise set it to shippingCost.
  • total := subtotal + shipping.
  • Always include all three lines (Subtotal, Shipping, Total). When shipping is free, the line shows $0: for a subtotal of 120 the lines would be Subtotal: $120, Shipping: $0, Total: $120.
  • Join the three lines with a newline "\n" into one string result, then print it with fmt.Println(result).

Decide shipping in an if/else first, then build the three lines.

Declare var shipping int before the if, so it is still visible after the if/else, then assign it in both branches.

Use strconv.Itoa(...) to convert each number to text before concatenating it.

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)

STEP 1 OF 11
    Ctrl + Enter to check

    GO · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V3 in Go, explained

    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

    1. Declare shipping before the if. A variable created inside a block disappears when the block ends. The total and the lines are built after the if/else, so shipping must live outside it. Writing var shipping int creates it with a type and no value yet (Go fills in 0).
    2. Open an if on subtotal >= freeShippingThreshold. Go needs no parentheses around the condition, but the braces are required.
    3. Set shipping = 0 in the if block. This is the free-shipping case. It uses =, not :=, because the variable already exists. With := the block would create a new, separate shipping that vanishes at the closing brace.
    4. Add the else on the line of the closing brace. Go requires } else { on one line; a line break before else is a syntax error.
    5. Set shipping = shippingCost in the else block. This branch handles every cart under the threshold.
    6. Compute total. It is subtotal + shipping. When shipping is free, the total equals the subtotal.
    7. Build line1, line2 and line3. Each is a label followed by a number converted with strconv.Itoa. Go does not join a number to a string with +, so each number has to become text first.
    8. Join the lines into result with "\n" between them: two newlines for three lines.
    9. Print result with fmt.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: $" + subtotal does not compile, because + needs both sides to be strings. strconv.Itoa(75) returns "75". The program needs the strconv and fmt packages imported.
    • Branches assign, they don't declare. Both branches use = on the outer shipping. Every path through the if/else sets it, so it always has the value the later lines need.
    • Whole numbers only. All amounts are int here, 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: $0 and Total: $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 := 0 inside the if creates a new variable that hides the outer one. The compiler rejects the program with shipping 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 $0 on the shipping row.
    • Forgetting the conversion. Passing an int to a string join is a compile error, so nothing runs until each number goes through strconv.Itoa.

    Testing

    • Subtotal 75 prints shipping $10 and total $85.
    • Subtotal 120 prints shipping $0 and total $120.
    • Subtotal 100 prints shipping $0 and total $100.
    • Subtotal 99 prints shipping $10 and total $109.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free