Control Structures
We studied the grammar, primitive types and composite types. Now we are able to work with variables, but they are nothing without "algorithms" (without control structures). Let's dive in and see how we do what in Go.
Blocks
Blocks define the scope of constants, variables, functions and types. Therefore there are several blocks, where one of these can exist.
There is a package
, file
, function
, and {}
block.
- All constants, variables, functions and types declared outside of a function are in a
package block
. - All
imported
constants, variables, functions and types are calledfile block
. - All constants, variables, functions and types (parameters included) declared in a function are in a
function block
. - All constants, variables, functions and types declared in curly braces in a function are in a
{} block
.
Shadowing variables
Shadowing variables is setting the variable value shortly in a {}
block inside of a function block.
It only works if you use the same name of the "shadow" variable.
Here is an example:
the output is:
That's why it is so problematic to use :=
, we can shadow variables in inner scopes of a function, which makes it harder to really define, which value a variable has.
Danger
You can shadow imported package names. This can break your scope and you should never ever do this:
will output:
Universe Block
Golang has only 25 keywords (var, for, switch). Types (int, string), Constants (true, false) and Functions(make, close) are not included in the keyword list.
They were defined in a universe block
. This is the most outside block.
Because they are defined in the most outside block, they can be shadowed.
Danger
true
will be shadowed and can result in big misbehaviour of the code.
If
If is actually pretty easy and works in the same way as in other programming languages.
Scoping variables in if statements
If you want to declare a variable while checking for it's value you can declare a variable directly in an if statement. Variables defined as a scoped variable only exist in the defined scope.
For
There is only one looping keywod called for
.
With for you can define 4 different formats in Go:
- C-style for
- Condition for
- Infinite for
- for-range
C-style for
This is probably known to you.
The only thing to mention is that you can't use var
keyword to define i.
Condition for
Go has the ability to run a go loop forever until a certain variable because false
.
This pattern is known as while
loops in other programming languages:
Infinite for
The infinite for works the same way as for the condition for, you just have to leave the condition. This would force the for loop to run forever.
break and continue
break
helps you to stop a for loop.
continue
helps you to skip the rest of the scope and start a new loop
for-range
for-range
loops are designed to make a foreach loop in Go.
You can use strings, arrays, slices and maps with foreach loops.
Later on we will talk about Channels
wher for-range loops can be handy too.
output:
In a for-range loop you always get two variables. The first one is the index (strings, arrays, slices) or key (maps). The second one is the actual value.
If you don't need the key or value in a for-range loop, you can use _
to let Go know, that it should ignore this variable.
output:
If you just want the key, you can use the first variable and leave the second not declared.
for-range variables are copies
Go iterates over copies of your variable. Therefore modifying the for-range variables directly is useless. You have to overwrite outer scopes variables:
output:
Labeling for statements
You can break
or continue
in for loops by using labels
.
This example continues the outer for loop.
You will find labeled for loops very rare in the Go ecosystem.
output:
switch
Switch statements in Go are very useful though in other languages they are more avoided.
Let's see an example:
output:
a is less than 5 characters long
hello is 5 characters long
go is less than 5 characters long
javascript is more than 5 characters long
Duplicate cases
You cannot define multiple cases:
Break switch in for loop
Sometimes you have to break a for loop.
But break
inside a switch would result in breaking the switch scope and not the for loop.
In that case you can use labels to break explicitly the loop.
output:
0 is even
don't know what to do
2 is even
3 is divisible by 3
4 is even
don't know what to do
6 is even
exit
8 is even
9 is divisible by 3
We can fix that by using labeled for loops:
output:
0 is even
don't know what to do
2 is even
3 is divisible by 3
4 is even
don't know what to do
6 is even
exit
Blank Switches
You can use a variable to switch on the value of it or use a blank switch to switch for boolean expression:
Surely the first one is more explicit.
goto
Go has the support to use goto
statements.
You will probably never use goto, but I will show an example here anyway:
output: