What You are Going to Learn?

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.

Important

You must have completed the Variables lesson before you will be able to complete this lesson.

Declaring a Variable

To declare or create a variable you need three pieces of information. They are

  • The var keyword that is used to introduce a variable.
  • The variable name, so that you can refer to it later
  • The variable type which describes the type of data that the variable can hold.

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, string.

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 names.

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 and string for a string type.

Now it is your turn

How would you write a variable declaration for

  • A number variable named distance_to_London
  • A string variable called capitalOfScotland

The variable declarations would be:

var distance_to_London int
var capitalOfScotland string

The Rules for Variable Names

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

  • You cannot use a keyword as a variable name.
  • You cannot use spaces in your variables names, or any other symbols like ().
  • You cannot use a package name for a variable name.
  • You cannot start a variable name with a number.
  • You cannot use the same name as another variable. Each variable name has to be unique.
  • You can only use the letters a to z or A to Z or the numbers 0 to 9 or the underscore character _.

If a variable name does not follow these rules it is illegal and Go will not let you use it.

Assigning a New Value To a Variable

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 to 8. 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 equals, = 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.

Important

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.

The badvariables Program

Look at the badvariables program

 1package main
 2
 3import "fmt"
 4
 5func main() {
 6	var short_side int
 7	var long side int
 8	perimeter int
 9	var area string
10
11	short_side = 3
12	long_side % 8
13
14	fmt.Println("What shape am I?")
15	fmt.Print("Two of my sides are ")
16	fmt.Print(shortside)
17	fmt.Println( meters long.")
18	fmt.Print("Two of my sides are )
19	fmt.Print(long_side)
20	fmt.Println(" meters long.")
21	perimeter = 2*short_side + 2*long_side
22	fmt.Print("My area perimeter is ")
23	fmt.Print(perimeter)
24	fmt.Println(" meters.")
25	area = short_side * long_side
26	fmt.Print("My area is ")
27	fmt.Print(area)
28	fmt.Println(" meters square.")
29}
Fig-1. The uncorrected badvariable code

The badvariables program has lots of mistakes in it, so many it won’t run.

Now it is your turn

How many mistakes can you find in the badvariabels program?

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:

  • Line 7 - the variable name long side in the variable declaration is illegal because it contains a space. Lines 19, 21 and 25 use the correct and intended spelling
  • Line 8 - the variable declaration is missing the var keyword
  • Line 12 - the wrong symbol is used for variable assignment. A percentage, %, symbol has been used instead of the correct equals sign, =
  • Line 16 - the variable name shortside has not been defined. This is a typing error of the correct variable name short_side
  • Line 17 - the string in the fmt.Println is missing the opening inverted commas.
  • Line 18 - The string in the fmt.Print is missing the closing inverted commas.
  • line 25 - The variable area is declared on line 9 to be a string. But the value of the expression short_side * long_side is an integer number. The mistake is actually on line 9. area should have been declared as an int number type not a stringtype.

We were sneaky and also put some mistakes in the fmt.Println lines. 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 string. 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

The corrected program is

 1package main
 2
 3import "fmt"
 4
 5func main() {
 6	var short_side int
 7	var long_side int
 8	var perimeter int
 9	var area int
10
11	short_side = 3
12	long_side = 8
13
14	fmt.Println("What shape am I?")
15	fmt.Print("Two of my sides are ")
16	fmt.Print(short_side)
17	fmt.Println( meters long.")
18	fmt.Print("Two of my sides are "")
19	fmt.Print("long_side)
20	fmt.Println(" meters long.")
21	perimeter = 2*short_side + 2*long_side
22	fmt.Print("My area perimeter is ")
23	fmt.Print(perimeter)
24	fmt.Println(" meters.")
25	area = short_side * long_side
26	fmt.Print("My area is ")
27	fmt.Print(area)
28	fmt.Println(" meters square.")
29}
Fig-2. The corrected badvariable code
The shape being described is obviously a rectangle.

The hellome Program

This is the hellobob program from the last lesson

 1package main
 2
 3import (
 4	"fmt"
 5)
 6
 7func main() {
 8	var name string
 9	var age int
10
11	name = "Bob"
12	age = 8
13
14	fmt.Println("The hellobob program shows you how to use variables.")
15	fmt.Println("")
16
17	fmt.Print("Hello, my name is ")
18	fmt.Print(name)
19	fmt.Println(".")
20	fmt.Print("I am ")
21	fmt.Print(age)
22	fmt.Println(" years old.")
23}
Fig-3. The hellobob.go code

You are going to change it to create two new programs.

The hellome Program

Now it is your turn

Now it is your turn to write a program to display your name and your age. We will call this program hellome.go

You will need to start your text editor and edit the program yourself. Remember to save it as hellome.go in a new hellome directory.

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 called age and name. Look at lines 11 and 12.

Remember to change the first fmt.Println line on line 14 so that it says hellome and not hellobob.

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.

The hellofriend Program

Now it is your turn

Once you have the hellome.go program working we want you to write another program called hellofriend.go.

We want 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 hellofriend directory.

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 hellome program.

You also need to change the Println and Print lines to print their name and age as well as your name and age.

You also need to change the Println lines at the start of the program.

Search

Featured Lesson

Numbers

What You are Going to Learn?

Computers are used to process data. All data is made up of numbers. Yes, really! Everything is just a bunch of numbers to a computer. These are the only things they understand.

We are going to explain how numbers are used in Go programs. Then we are going to show you how to do type sums in Go.