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
- Compute
totalasprice * quantity. - Build
line1:item, then the literal" x"(with its leading space), thenstrconv.Itoa(quantity). - Build
line2: the literal"Total: $"followed bystrconv.Itoa(total). - Join the lines into
resultwith"\n"between them. The newline escape is a single character that moves the text after it to a new row. - Print
resultwithfmt.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 := 12makes anintanditem := "Coffee Mug"makes astring. Multiplying twointvalues gives anint, sototalis aninttoo. - No implicit conversion. Go never converts a number to a string for you inside
+. Writing"Total: $" + totalfails to compile with a type mismatch betweenstringandint. strconv.Itoa, notstring(total).string(rune(65))is"A", because an integer converted withstringis read as a character code.strconv.Itoa(65)is"65".- Whitespace lives in the literals. The space before the
xis part of" x". The$has no special meaning in a Go string. - Newline handling.
fmt.Printlnprints its argument and then adds its own newline. The one insideresultseparates the two rows, and the one added byPrintlnends 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 Mugx3orCoffee Mug x 3fail. The row is the item, one space,xand 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 := 0the output isCoffee Mug x0andTotal: $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 x3andTotal: $36. - With
item := "Pen",price := 4andquantity := 10, the output isPen x10andTotal: $40. - With
quantity := 0, the output isCoffee Mug x0andTotal: $0.
