Menu Close


Go: Control flow

Decision Statement

The decision statement in Go like in many other languages is the “if” statement. This statement is using a conditional expression that evaluate to a Boolean value. When the value is true the next statement or block of statements is executed otherwise is not executed and skipped over.

The condition do not have to be included in parentheses ( ) like in some other programming languages. After the condition we use braces { } that are mandatory to create a block of code with several statements.

Example of decision with a single block:

The local scope

The “if” statement has a local scope. That is we can define a local “control variable” to be used inside the if block. That may be harder to comprehend. In the next example variable v is in to local scope of the “if” statement.  Variables declared by the statement are only in scope until the end of the “if”.  If we try using v after the last return statement we will have an error.

Example of local scope:

Two path decisions

For creation of two blocks of code in decision statement we use “else” keywordThis is also known as two deck conditional statement. Variables declared inside an “if” simple statement are also available inside “else” block.


Go use only one keyword for all iterations: the “for” keyword. This can define a finite or infinite loop.

Basic loop

The basic “for loop” has three components separated by semicolons:

  1. the init statement: executed before the first iteration
  2. the condition expression: evaluated before every iteration
  3. the post statement: executed at the end of every iteration

The init statement will often be a short variable declaration, and the variables declared there are visible only in the scope of the for statement. The loop will stop iterating once the Boolean condition evaluates to false. The “init” and “post” statement are optional.

Note: Unlike other languages like C, Java, or Javascript there are no parentheses surrounding the three components of the  for statement. The braces { } are always required.

Example of for with no “init: and no “post”. It may be curios how the single semicolon is still necessary. So it looks like a conditional between two semicolons.

The while loop

To simulate a while loop we use for with a post conditional  and no control variable:

Infinite loop

To simulate an infinite iteration we use a for loop with no condition and no control variable:


The switch statement is a multi-path conditional selection statement. This allow program to execute on different logical path depending on value of one variable. A switch statement use 2 multiple keywords to create a multi-case construct. The keywords are: {switch, case, default, fallthrough}

Value based switch

The switch use a control variable that is local to the case statement. In next example we use “os” variable to analyze the operating system. Switch cases evaluate cases from top to bottom, stopping when a case succeeds.

When a case control variable has a particular case value switch breaks automatically and no other case is executed except when the case last statement is a fallthrough statement. The last is the “default” case is executed when variable has a different value from any other cases.

Conditional switch

Switch without a condition is the same as “switch true”. This switch can be used to create a multi-deck conditional construct similar to multiple cascading if statements. This construct can be a clean way to write long if-then-else chains.


One Go program can execute normally or can have and error. Go programs create error state with “error” values. Functions often return a value of type “error”. Calling code should handle errors by testing whether the “error” equals nil value. The error type is a built-in interface similar to fmt.Stringer;

As with fmt.stringer the fmt package looks for the error interface when printing values.

A nil error denotes success; a non-nil  error denotes failure.