Control structure in Go

Lucien Makutano
3 min readSep 14, 2020

--

How Language Affects Decision-Making — TranslateMedia

Like in any programming language, control structures are used in deciding the flow of the program, also to execute repetitively some blocks of code. In the quest for simplicity, Go provides less yet powerful control structures.

Conditional control Structure

Go provides us with two conditional control structure, IF and SWITCH statements.

IF statement

IF statements are used to assert whether the condition meets the requirement. Whether it is true or false.

if syntax

as simple as that😮😮. Now let’s try to check whether a number is even.

if example

In programming languages, the double equal(==) signs are used for comparison and the single equal(=) sign is used for assignment(giving value to the allocated memory through the variable // age:= 19). The % sign returns the remainder of a division. More on comparison operators

Like every other programming languages, Go gives us a way to deal with cases where the condition is not met and that is through the else keyword.

if else syntax

Note that the else keyword is put on the same line as the closing curly brace from the if that is because Go code follows a specific coding style and also to resolve the conflict between programmers of where to put the curly braces, ah… what a wonderful community😇😇. Quick question which one is better? tabs or spaces🤣🤣.

Multiple conditions can be tested too using the IF-ELSE structure.

if syntax

In the article on variables and constants, it was mentioned that when retrieving data from a map, the Go compiler does not flag an error if the key does not exist but instead returns a boolean value. Let’s use the concept of IF-statement to check whether a key exists or not.

map key check

Practice IF-statement.

Go will return false if the key does not exist and true otherwise. The !(not) operator inverts the value of the exist variable so the if statement is checking whether the invert of exist is true. Line 15 can be literally read as if does not exist.

Switch

The SWITCH statement in Go is a bit special. It does what SWITCH statements do in other programming languages plus an additional check🤔🤔. Go switches work with cases.

switch syntax

Practice SWITCH.

The first case checks whether the value of name is “twist”, the second case whether it is “tado” lastly the default case executes if there are no cases suited.

For those who have used SWITCH statements from other programming languages would wonder but where is the break statement? wouldn’t that execute all the cases? The way Go was designed, it assumes the presence of the break statement.

What happens if I want more than one case to execute the same piece of code as I would do for instance in Java by omitting the break statement?

switch example

If any case matches, it will chain the execution to the other cases until it finds a break statement but since it is not there all the three cases will execute. You will agree with me that this is a tedious work plus it will get messy after sometimes and that is where Go’s single-line multi-value check condition comes in.

multi value check

much cleaner now😇😇.

Iterative Control Structure

Loops

Unlike other programming languages, Go has only one way of iterating through a collection of elements. For-loop is the only type of loop that exists in Go powered by the range function.

loop with range

Go does not allow unused variables, but provides a placeholder _ to be used instead. Let’s suppose that only the value is needed and not the index. The above code would be rewritten as follow.

the blank identifier

By using the underscore also called the blank identifier in the place of the index, we are telling the Go compiler to ignore that value. Go also support the traditional For-loop plus other variation of it.

different types of loop implementation

Practice loop.

Conclusion

Go offers a concise yet powerful number of control structures to help developers spend more time building cool program.

--

--