You are going to practice declaring and assigning values to variables in this lesson. This will help you learn the patterns for variable declaration and variable assignment.
You must have completed the Variables lesson before you will be able to complete this lesson.
To declare or create a variable you need three pieces of information. They are
varkeyword that is used to introduce a variable.
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 a legal one according to the rules for variable
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.
How would you write a variable declaration for
The variable declarations would be:
var distance_to_London int var capitalOfScotland string
When we say that a variable name is legal we mean that it must follow these rules. These rules are part of Go’s syntax rules.
Remember, the rules for variable names are
Zor the numbers
9or the underscore character
If a variable name does not follow these rules it is illegal and Go will not let you use it.
To assign or set, or change the value of a variable you need
to use an equals
= sign like this
age = 8
Which sets the value of
age must have been declared as
var age int
before the assignment can happen.
There is a pattern you can use to remember how to set the value of a variable. The pattern is
name-of-variable = new-value
First you use the name of the variable that you want to set, then an
= sign and then the new value for the variable.
You can only set a variable to a value that makes sense according to the
type of the variable. If you tried to set the variable
age to the string
"eight" like this
age = "eight"
it will not work.
If a variable was declared as an
int type then you can only store
numbers in it. If the variable was declared as a
string type you can
only store strings in it.
When you change the value of a variable the old value is lost, forever. You can not get it back, the new value will overwrite the old one.
Look at the
badvariables program has lots of mistakes in it, so many it won’t run.
How many mistakes can you find in the
Can you work out what the mistakes are and fix them so that the program runs?
There are 7 mistakes in this program. The mistakes are:
long sidein the variable declaration is 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 typing error of the correct variable name
fmt.Printlnis missing the opening inverted commas.
fmt.Printis missing the closing inverted commas.
areais 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.
We were sneaky and also put some mistakes in the
The last mistake, the one on line 25, is hard to find, but easy to fix once you understand what is happening. The program is trying to assign
a number, an
int, to a variable that has been declared as a
So either line 25 is wrong, or the declaration on line 9 is wrong. You
have to work out which is the case. In this example, it is line 9, the declaration,
that is wrong. You want
area to be an
int type because you are trying
to store the answer of a sum in it.
So you just have to apply the fix at the top of program not the bottom
This is the
hellobob program from the last lesson
You are going to change it to create two new programs.
Now it is your turn to write a program to display your name and your age.
We will call this program
You will need to start your text editor and edit the program yourself. Remember to
save it as
hellome.go in a new
Can you change the program to print your name and your age and make it run?
To do this you need to change the values assigned to the variables
name. Look at lines 11 and 12.
Remember to change the first
fmt.Println line on line 14 so that it
hellome and not
If you get stuck look at the
hellobob program. It will show you how to declare
variables, assign values to them and print them out.
Remember to make little changes to your program. Then save and run the program after each change to make sure that you have no errors.
Once you have the
hellome.go program working we want you to write another
hellofriend to print out your name and age and the name and age
of one of your friends.
To do this you need to save your working
hellome program as
hellofriend.go in a
Now you need to edit
hellofriend.go so that when you run it, it prints
out something like this.
The hellofriend program prints my name and age and my friend's name and age. Hello, my name is Bob. I am 8 years old. My friend's name is Paul Paul is 9 years old.
This output is for Bob whose friend is called Paul. Paul is 9 years old. If you get stuck look at the hint.
You need to create two more variables. One for your friend’s name
and one for your friend’s age. Just like you did for your name in the
You also need to change the
You also need to change the
Println lines at the start of the program.