Chapter 1 - Syntax Basics
Exercise 1: Fill in the Blanks
The program below should take a number of cans of food and calculate their total weight. It should declare a count
variable with the number of cans, and a unitWeight
variable with the amount (in kilograms) that each can weighs. It should then multiply count
by unitWeight
, and assign the result to a new totalWeight
variable. Finally, it should print the number of cans and their total weight.
Fill in the blanks in the program so that it will run and produce the specified output. Note that count
will have a type of int
, but unitWeight
will have a type of float64
, so you’ll need to do a conversion before you can multiply the two together.
Output:
20 cans weigh 8 kilograms
When you’re ready, have a look at the solution.
Exercise 2: Code Rework
Several of the variable names used in this example break Go naming conventions. The code is also somewhat longer than it needs to be. See if you can modify so that it’s shorter and follows conventions better, but still produces the same output.
Output:
503.7
When you’re ready, have a look at the solution.