Bitpath

Boss lesson · Unit 01

A receipt

Prices, a discount and tax become a formatted total, and the compiler gets a say.

13 exercisesabout 3 min

What you practise

  • Prices, quantities, a discount and tax turned into a formatted total with fmt.Printf
  • Reading the compiler's first error messages

Try a question from this lesson

Pick one

A café sells 4 coffees at 2.50 each. This works out the cost before any discount. What does it print?

Go
price := 2.5
qty := 4
subtotal := price * float64(qty)
fmt.Println(subtotal)

The ideas

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

Printf: printing to a pattern

fmt.Printf prints a pattern with gaps in it, and fills the gaps with the values that follow, in order.

%d is a gap for a whole number. %.2f is a gap for a decimal number, shown with exactly two decimals. %s is a gap for a string.

Printf does not start a new line by itself: end the pattern with \n to do that.

Go
fmt.Printf("Total: %.2f\n", 10.0)
// Total: 10.00
fmt.Printf("%d x %s\n", 4, "Coffee")
// 4 x Coffee

Concept

Reading a compiler error

When Go refuses a program, it says where and why. Read the message in parts: the file, the line number, how far along that line, and then what is wrong.

Go to that line first. If there are several errors, fix the top one and compile again: the later ones are often caused by the first.

./main.go:7:10: undefined: subtotl

./main.go  the file
7          the line
10         how far along the line
undefined  Go does not know this name

10 more exercises in the app

  • 4× type the answer
  • 1× fill the blanks
  • 3× multiple choice
  • 1× order the lines
  • 1× true or false

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy