What You are Going to Learn

You saw in the last lesson how to do simple sums using Go. Now we want to show you that sums in Go follow the same mathematical rules that you already understand.

Maths is a universal language, even for computers.

Lastly we are going to show you the pattern you need to print a sum and its answer to the screen.

Sums in Go

Sums in Go are pretty similar to the sums you use everyday in maths. You can do addition, subtraction, multiplication and division.

Now it is your turn

Can you remember the symbols that Go uses for

  • addition
  • subtraction
  • multiplication
  • division

The answers are

  • addition is +
  • subtraction is -
  • multiplication is *
  • division is /

Remember that you type sums in Go just the same as you would write them normally, apart from using * for multiplication and / for division.

Now it is your turn

How would you write these sums in Go?

three add four
six subtract one
five multiplied by six
ten divided by two

The answers are

3 + 4
6 - 1
5 * 6
10 / 2

Multiplication before Addition

Maths in Go behaves in exactly the same way as maths everywhere else. So if you see a sum like this

3 + 2 * 5

You know that the answer is 13, not 15. You have to multiply the two and the five before you add the three. You have to do the multiplication before addition or subtraction.

So what about division? It’s almost the same. You have to do division before subtraction or addition. So if you see a sum like this

25 - 20 / 4

You know the answer is 20.

Now it is your turn

See if you can solve these sums

4  * 4 + 2
30 / 2 + 5
4  * 5 / 2

The answers are:

4 * 4 + 2 = 18
30 / 2 + 5 = 20
4 * 5 / 2 = 10

In the last sum, the multiplication and division are considered equally important, so you need to work from the left to the right like this.

4 * 5 / 2 is 20 / 2 = 10

Go calls these rules the rules of operator precedence. The rules can be summarised as, do any multiplication or division first, then any addition or subtraction.

Brackets or How to Change the Operator Precedence

But what do you do if you really do want

3 + 2 * 5

to equal 13 and not 18?

Well the answer is to use brackets, the ( and ) on your keyboard. They are typed Shift+9 for ( and Shift+0 for ).

Brackets are used to group terms together. Think of terms a mini-sums. The rule for using brackets is simple. You have to solve the sum in the brackets first.

So we can rewrite

3 + 2 * 5

with brackets like this

3 + (2 * 5)

Which gives the answer 13. Or this

(3 + 2) * 5

which gives the answer 15.

Now it is your turn

See if you can solve these sums

4 * (4 + 2)
(28 - 20) / 4
30 / (2 + 5 + 3)

The answers are

4 * (4 + 2) is 4 * 6 = 24
(28 - 20) / 4 is 8 / 4 = 2
30 / (2 + 5 + 3) is 30 / 10 = 3

The Extra Numbers Program

Let’s write a Go program to show you this.

Open your terminal or command prompt. We are going to put each Go program in its own directory. This is the recommended practice for Go programs. In your terminal you need to change to the location of your Go Workspace. To do this type

On Linux, Raspberry Pi and Mac OS X

cd $GOPATH/src/

On Windows

cd %GOPATH%\src\

Now you need to make a new directory. We need to call this extra-numbers after the program we will write. Then we need to change directory into the new extra-numbers directory.

mkdir extra-numbers
cd extra-numbers

Now you need to start you editor, either Atom or LiteIDE

On Linux, Windows and MacOS X

atom numbers.go

On Raspberry Pi

liteide numbers.go

The extra-numbers.go tells Atom or liteIDE start with the file extra-numbers.go open in the editor. If the file does not exist the editor will create it for you.

Once your editor starts you can type in the extra-numbers.go program.

Type carefully

Remember to type in extra-numbers.go exactly as we have it here.
 1package main
 2
 3import "fmt"
 4
 5func main() {
 6    fmt.Println("The extra-numbers program shows you how to add, subtract")
 7    fmt.Println("multiply and divide numbers and how to use brackets.")
 8    fmt.Println("Sums without brackets.")
 9    fmt.Print("3 + 2 * 5 = ")
10    fmt.Println(3 + 2 * 5)
11    fmt.Print("4 * 4 + 2 = ")
12    fmt.Println(4 * 4 + 2)
13
14    fmt.Print("25 - 20 / 4 = ")
15    fmt.Println(25 - 20/4)
16
17    fmt.Print("30 / 2 + 5 = ")
18    fmt.Println(30 / 2 + 5)
19
20    fmt.Println("Sums with brackets.")
21    fmt.Print("3 + (2 * 5) = ")
22    fmt.Println(3 + (2 * 5))
23
24    fmt.Print("(3 + 2) * 5 = ")
25    fmt.Println((3 + 2) * 5)
26
27    fmt.Print("4 * (4 + 2) = ")
28    fmt.Println(4 * (4 + 2))
29
30    fmt.Print("(28 - 20) / 4 = ")
31    fmt.Println((28 - 20) / 4)
32
33    fmt.Print("30 / (2 + 5 + 3) = ")
34    fmt.Println(30 / (2 + 5 + 3))
35}
Fig-1. The extra-numbers.go code

Once you have typed the program in, you need to save it. Once you have saved it you need to run it with:

go run extra-numbers.go

If you typed the program correctly you should see

The extra-numbers program shows you how to add, subtract
multiply and divide numbers and how to use brackets.

Sums without brackets.
3 + 2 * 5 = 13
4 * 4 + 2 = 18
25 - 20 / 4 = 20
30 / 2 + 5 = 20
Sums with brackets.
3 + (2 * 5) = 13
(3 + 2) * 5 = 25
4 * (4 + 2) = 24
(28 - 20) / 4 = 2
30 / (2 + 5 + 3) = 3
The first thing to notice is that the extra-numbers program is very similar to the previous numbers program.

Both programs look similar, they have a similar structure.

The first interesting lines are the fmt.Println function is used in Lines 6, and 7.

fmt.Println("The numbers program shows you how to add, subtract")
fmt.Println("multiple and divide integer numbers.")

This lines produce the following lines in the output.

The extra-numbers program shows you how to add, subtract
multiply and divide numbers and how to use brackets.
Sums without brackets.

This is exactly the same behaviour as you saw in the numbers program. Whatever appears between the inverted commas is printed out in the terminal window. The only difference is the content of the lines that are printed.

The remaining lines all follow the same pattern of a fmt.Print line followed by a fmt.Println line. For example Lines 9 and 10 are

fmt.Print("3 + 2 * 5 = ")
fmt.Println(3 + 2 * 5)

In this pattern the first fmt.Print line prints the sum that will be calculated. The second fmt.Println line prints the result of the sum.

Important

Notice the lack of inverted commas in the Println line. This tells Go not to print the sum. Instead Go works out the result of the sum, more correctly called the expression, before printing the answer.

Like this

fmt.Println(3 + 2 * 5)

prints the answer of 3 + 2 * 5 which is 13 and not 3 + 2 * 5.

The rest of the program follows the same pattern. The sums change but the pattern is the same.

Both programs start with the same five lines.

package main

import "fmt"

func main() {

As before these lines declare the main package, line 1, and a main function on line 5. The program also uses the fmt package, short for format. The fmt package provides the Print and Println functions that the program uses to display the sums and the answers in the terminal window when the program runs.

You will see lines like this at the start of every Go program. Don’t worry if you don’t understand these at the minute. You just need to know that you need them.

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.