Start learning free

LESSON 189 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V1 in Go: A One-Item Receipt

Practice Read the full explanation

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

    GO · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V1 in Go, explained

    Problem overview

    This project lesson prints a tiny receipt for a single item. The starter gives three variables: item := "Coffee Mug", price := 12 and quantity := 3. The program computes the total, builds two lines of text and prints them:

    Coffee Mug x3
    Total: $36
    

    The first line is the item, a space, the letter x and the quantity. The second line is Total: $ followed by the total, which is the price times the quantity. The two lines are joined with a newline into one string, result, and printed with a single fmt.Println.

    Approach: numbers first, then text

    The work splits in two. First comes arithmetic on integers: total := price * quantity. Then comes text: each line is a string assembled with + from literal pieces and the values.

    The only Go-specific obstacle is that + does not join a string and an int. Both sides must be strings. The numbers quantity and total therefore go through strconv.Itoa, which returns the decimal digits of an int as a string. The item and the labels are strings already.

    Step by step

    1. Compute total as price * quantity.
    2. Build line1: item, then the literal " x" (with its leading space), then strconv.Itoa(quantity).
    3. Build line2: the literal "Total: $" followed by strconv.Itoa(total).
    4. Join the lines into result with "\n" between them. The newline escape is a single character that moves the text after it to a new row.
    5. Print result with fmt.Println.

    The solution in Go

    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)
    

    The code uses the fmt and strconv packages.

    The details worth knowing

    • Types are inferred. price := 12 makes an int and item := "Coffee Mug" makes a string. Multiplying two int values gives an int, so total is an int too.
    • No implicit conversion. Go never converts a number to a string for you inside +. Writing "Total: $" + total fails to compile with a type mismatch between string and int.
    • strconv.Itoa, not string(total). string(rune(65)) is "A", because an integer converted with string is read as a character code. strconv.Itoa(65) is "65".
    • Whitespace lives in the literals. The space before the x is part of " x". The $ has no special meaning in a Go string.
    • Newline handling. fmt.Println prints its argument and then adds its own newline. The one inside result separates the two rows, and the one added by Println ends the output.

    An alternative: formatted printing

    The same output can come from one formatted call. fmt.Printf("%s x%d\nTotal: $%d\n", item, quantity, total) prints Coffee Mug x3 and Total: $36. The verb %s inserts a string, %d inserts an integer, and unlike Println, Printf adds no newline of its own, so the format string ends with \n. The lesson builds the strings by hand, which makes each piece of the receipt visible as a variable and gives practice with + and strconv.Itoa.

    Edge cases and common mistakes

    • Spacing. The tests compare the text exactly, so Coffee Mugx3 or Coffee Mug x 3 fail. The row is the item, one space, x and the number.
    • Forgetting a conversion. This is the usual compile error here: one of the two numbers joined to text without strconv.Itoa.
    • Zero quantity. With quantity := 0 the output is Coffee Mug x0 and Total: $0.
    • Unused variables. Go refuses a variable that is declared and never read. All the variables here are read, which is why the receipt needs no extra lines to keep the compiler quiet.

    Testing

    • The starting data prints Coffee Mug x3 and Total: $36.
    • With item := "Pen", price := 4 and quantity := 10, the output is Pen x10 and Total: $40.
    • With quantity := 0, the output is Coffee Mug x0 and Total: $0.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free