Start learning free

LESSON 162 OF 404 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V1 - Single Item Total in Go

Practice

Practice variables, arithmetic, and string concatenation by printing a tiny one-item receipt.

You start with:

item := "Coffee Mug"
price := 12
quantity := 3

Build:

Coffee Mug x3
Total: $36

Rules:

  • Compute total := price * quantity.
  • First line: "<item> x<quantity>" (e.g. "Coffee Mug x3").
  • Second line: "Total: $<total>".
  • Join the two lines with a newline "\n" into one string result, then print it with fmt.Println(result).

Wrap each integer with strconv.Itoa(...) before concatenating it with +.

line1 + "\n" + line2 joins both lines into one string.

item := "Coffee Mug"
price := 12
quantity := 3

total := price * quantity
line1 := item + " x" + strconv.Itoa(quantity)
line2 := "Total: $" + strconv.Itoa(total)
result := line1 + "\n" + line2
fmt.Println(result)

STEP 1 OF 5
    Ctrl + Enter to check

    Shopping Cart V1 in Go

    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free