Flow control

Flow control is referred as the ability to decide which portion of code or how many times you execute some code on a condition. In Go, it is implemented using familiar imperative clauses like if, else, switch and for. The syntax is easy to grasp. Let´s review major flow control statements in Go.

The if... else statement

Go language, like most programming languages, has if…else conditional statement for flow control. The Syntax is similar to other languages but you don't need to encapsulate the condition between parenthesis:

ten := 10 
if ten == 20 { 
    println("This shouldn't be printed as 10 isn't equal to 20") 
} else { 
    println("Ten is not equals to 20"); 
} 

The else...if condition works in a similar fashion, you don't need parentheses either ...

Get Go Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.