To practice using the patterns for integer and string type variables in Go.
The objective is to get the class to write a Go program that use variables.
Creating these program will require the class to use the text editor (either Atom or LiteIDE) to create and save the source code file and the terminal/command line to run their program.
The lesson reinforces the patterns used for both variable declaration
and variable assignment. The pupils practice these patterns by
correcting a program that does not follow the rules and changing the
hellobob program to produce two new programs.
The class should be able to:
The example program is sufficiently simple that the little or no additional teaching should be required beforehand.
Variables. The programs demonstrate how to declare a variable, assign a value to it and how to use the variable. Variables are stored in the computer’s memory so there is a direct link to computer hardware. Sequences. The program demonstrates a sequence of instructions that are executed in order to print the results to the terminal window.
There is a direct link to mathematics where the concept of a variable or an unknown originates. There is a second link to mathematics through one of the example programs to shapes and their properties. There is also a link to science/astronomy in the plenary.
The three things that a variable declaration requires are
varkeyword that is used to introduce a variable.
They are required in this order.
The programmer can choose the name of the variable, and the variable’s
type. The variable name can be anything, provided the variable name
follows the rules. The programmer chooses between
int for a number type
string for a string type.
The variable declarations would be:
var distance_to_London int var capitalOfScotland string
Each part of the variable declaration must be stated explicitly by the programmer.
The pattern for variable declaration is always
var name-of-variable type-of-variable
each part is separated by a space. The declaration must appear on a line on its own.
var is the keyword for a variable declaration. It must be in lower case
and must be the first part of the variable declaration.
type-of-variable is the type to be used for the variable. At the moment
this can only be a number type,
int, or a string type,
name-of-variable is the name to be used for the variable. The variable
name that is used must be legal one according to the rules for variable
names introduced earlier.
Although the programmer can choose the variable names they use they must follow the rules for valid variable name. The rules are
Zor the numbers
9or the underscore character
Pupils should be encouraged to use meaningful variable names, that reflect the content of data and the use that the variable will be put to.
Computer program are read by other programmers many times before they are executed by a programmer. Meaningful variable names, that reflect the context of the data held by the variable will improve readability of the program significantly.
The value of a variable is changed when a new value is assigned to it. The
symbol for this is an equals sign,
You cannot assign a value of one type to a variable of another type. So you cannot assign a string value to variable that is declared to hold a number and vice-versa.
When a new value is assigned to a variable, the old value is lost. The new value overwrites the old value. The old value cannot be recovered.
Variable assignment sets the value of the variable.
The pattern for variable assignment is always
name-of-variable = new-value
Each part is generally separated by a space, though this is not a requirement.
name-of-variable is the name of the variable as it was previously declared.
new-value is a new value of the same type as the type the variable was
declared with. A string can only be assigned the value of another string.
A number can only be assigned the value of another number.
The previous value of the variable is replaced by the new value.
badvariable program is
There are 7 mistakes in this program. The mistakes are:
long sideis illegal because it contains a space. Lines 19, 21 and 25 use the correct and intended spelling
%symbol has been used instead of the correct equals sign,
shortsidehas not been defined. This is a miss typing of the variable name
fmt.Printlnis missing an opening inverted commas.
fmt.Printon is missing a closing inverted commas.
areais a declared on line 9 to be a string. But the value of the expression
short_side * long_sideis an integer number. The mistake is actually on line 9.
areashould have been declared as an
intnumber type not a
The corrected program is
The shape being described is obviously a rectangle.
There are two things that you and the pupils should notice in attempting to
The first is that the program has to be correct, the syntax has to be correct everywhere. The incorrect syntax in the two strings on lines 17 and 18 alone would have prevented the program from running.
The second is that the line that needs to be fixed, may not be the line
where the error occurs. This was the case with the
area variable. The problem
is on line 25 but the solution is in line 9. The programmer’s intention and the
expression of their intention do not match in this case. Line 25 implies that
area should be a number -
int type. But line 9 states that
area is a
type. One of these lines is wrong. There should be a logical alignment between the
the type of the value to be assigned to the variable and the type of the variable.
The number of patterns in programming and how they continually reappear in different programs should start to become apparent to the pupils.
There are many more patterns to learn and master.
The variables exercise involves changing the
hellobob program to create the
hellome program. The pupils have to change the variable values to create the
This shows the pupils how to change the variables and the effect this has when the program is run. It demonstrates that a change in one place, where the variables are assigned, effects the program’s behaviour in another place, where the variables are used in another
The pupils need to start with the
hellobob program from the
Here it is again
The pupils need to change two lines to effect the change. Assuming the pupil’s name is Alice and she is 7 years old these are the changes that the pupils need to make.
The first change is at line 11. The pupils need to replace the string
their name, in this case Alice.
The second change is at line 12. The pupils need to change the value of the age
variable to match their age. In this case the
age variable should be assigned
the value 7.
The changed lines 11 and 12 are therefore.
name = "Alice" age = 7
These are the only two lines that must be changed to turn the
program into the
The hinted at third change is very subtle. Line 14 is currently
fmt.Println("The hellobob program shows you how to use variables.")
Except this is no longer the
hellobob program. This is now the
program. This line is now misleading, especially when the program is run.
This is a trivial example of a bad user interface. The user interface to the program needs to be both clear and accurate. This is no longer accurate. So it should be changed. Just think about how much confusion this could cause to pupils if they look at this program again in a few weeks.
hellome program should look like this.
Which produces the following output
The hellome program shows you how to use variables. Hello, my name is Alice. I am 7 years old.
hellofriend program takes this one step further. Now the pupils have to
pick the names and the correct types of the additional variables for themselves.
They also need to edit the
Println lines to print the correct output. The pupils
have only the expected output to work from. They have to work backwards to work
out which lines to add to match the output.
The example shown in the slide is for Bob who is 8 and his friend Paul who is 9. The use of Bob as a pupil name again is coincidental.
The pupils should start with the
hellome program. To solve this the pupils need
to realise they need to add two variables to the program. One variable
to store their friend’s name and one to store their friend’s age.
Do do this we need two new variables. We have called them
myFriendsAge respectively. The pupils may choose different names. Their variable
names do not need to match ours.
myFriendsName is intended to hold a person’s name which is an English word so
this variable has to be of type
myFriendsAge is intended to hold a whole number so this has to be
int. The pupils have to use the correct types for their variables.
The following lines need to be added to the program
var myFriendsName string var myFriendsAge int
Immediately after the existing variable declarations for
Having declared the new variables the pupils need to set their values like so:
myFriendsName = "Paul" myFriendsAge = 9
Ideally after the existing variable declarations.
Now the pupils need to work backwards to produce the additional lines of output that are required. This will require them to build the output up incrementally.
The first new line in the output is
My friends name is Paul
This requires one
fmt.Print line to be added to print everything up to and
including the space after “is”. Like so:
fmt.Print("My friends name is ")
Their friend’s name is printed using a
fmt.Println line by passing the
myFriendsName to the
fmt.Println function. Like so
The second new line in the output is:
Paul is 9 years old.
This line has 4 parts. First the name of the friend, next the “ is “, next the
friend’s age and lastly the “ years old”. It needs to be created incrementally
using a combination of
fmt.Println functions like so:
fmt.Print(myFriendsName) fmt.Print(" is ") fmt.Print(myFriendsAge) fmt.Println(" years old.")
All of the
fmt.Println lines need to be added after the last
fmt.Println lien in the existing
There is an alternative solution to producing the same output. A pupil could have written these two lines instead.
fmt.Print("My friends name is Paul") fmt.Println("Paul is 9 years old.")
While this is correct in that it produces the same output, this is not the intent or what the program is attempting to demonstrate.
If a pupil does this let them complete the exercise. Then ask them to change the assignment of their friends name to something else. In other words change the line that looks like this:
myFriendsName = "Paul"
myFriendsName = "Rachel"
Now ask them to run the program again. When the output does not change ask the
pupil why? Direct them to think about using the values of the
myFriendsAge variables in the program instead of tying their
friends name and age as part of the string that will be displayed.
If a pupil does this, the effect could be shown and discussed with the class.
The only remaining change is to change
hellome in the first
in the program to
The final program should look like this
There is a valid alternative solution which advanced pupils may spot directly, or accidentally fall into. The alternative solution is correct but it does not require the creation of any additional variables.
The pupils may realise that the existing
age variables can be
reused later in the program simply by assigning them a different value late
in the program. This is OK because the original values of the variables are
never used again.
The alternative solution is this
The last point the
name variable needs to have the value
Bob is on line 19.
The last point the
age variable needs to have the value 8 is on line 22.
After these lines the values can be changed to the name and age of the
pupil’s friend. The original values are no longer required. The values are
changed in lines 25 and 29.
var distance_to_Van_Maanens_Star int var distance_to_Alpha_Centauri int var distance_from_Alpha_Centauri_to_Van_Maanens_Star int distance_to_Van_Maanens_Star = 12 distance_to_Alpha_Centauri = 4 distance_from_Alpha_Centauri_to_Van_Maanens_Star = distance_to_Van_Maanens_Star - distance_to_Alpha_Centauri fmt.Print("The distance between Alpha Centauri and Van Maanens Start is ") fmt.Print(distance_from_Alpha_Centauri_to_Van_Maanens_Star) fmt.Println(" light years. This is a very long way away!")
is a slightly more realistic example of how variables are used in practice. The fraction calculates the distance in light years between two stars. The distances are held as integers and the units is assumed to be light years.
The variable name are more realistic which may throw the pupils off.
The key part is the variable assignment
distance_from_Alpha_Centauri_to_Van_Maanens_Star = distance_to_Van_Maanens_Star - distance_to_Alpha_Centauri
This line has been typed across two lines on the slide only to fit the line on the slide. It would be typed on a single line in the program.
Which amounts to
distance_from_Alpha_Centauri_to_Van_Maanens_Star = 12 - 4
The answer is 8 light years - about 8 * 9 trillion = 72 trillion kilometers