Let's talk about flow control, starting with if/else.
If/Else
This works pretty much the same as you expect but the expression doesn't need to be surrounded by parentheses ().
funcmain(){x:=10ifx>5{fmt.Println("x is gt 5")}elseifx>10{fmt.Println("x is gt 10")}else{fmt.Println("else case")}}
$ go run main.go
x is gt 5
Compact if
We can also compact our if statements.
funcmain(){ifx:=10;x>5{fmt.Println("x is gt 5")}}
Note: This pattern is quite common
Switch
Next, we have switch statement, which is often a shorter way to write conditional logic.
In Go, the switch case only runs the first case whose value is equal to the condition expression and not all the cases that follow. Hence, unlike other languages, break statement is automatically added at the end of each case.
This means that it evaluates cases from top to bottom, stopping when a case succeeds.
Let's take a look at an example:
funcmain(){day:="monday"switchday{case"monday":fmt.Println("time to work!")case"friday":fmt.Println("let's party")default:fmt.Println("browse memes")}}
$ go run main.go
time to work!
Switch also supports shorthand declaration like this.
switchday:="monday";day{case"monday":fmt.Println("time to work!")case"friday":fmt.Println("let's party")default:fmt.Println("browse memes")}
We can also use the fallthrough keyword to transfer control to the next case even though the current case might have matched.
switchday:="monday";day{case"monday":fmt.Println("time to work!")fallthroughcase"friday":fmt.Println("let's party")default:fmt.Println("browse memes")}
And if we run this, we'll see that after the first case matches the switch statement continues to the next case because of the fallthrough keyword.
$ go run main.go
time to work!
let's party
We can also use it without any condition, which is the same as switch true.
x:=10switch{casex>5:fmt.Println("x is greater")default:fmt.Println("x is not greater")}
Loops
Now, let's turn our attention toward loops.
So in Go, we only have one type of loop which is the for loop.
But it's incredibly versatile. Same as if statement, for loop, doesn't need any parenthesis () unlike other languages.
For loop
Let's start with the basic for loop.
funcmain(){fori:=0;i<10;i++{fmt.Println(i)}}
The basic for loop has three components separated by semicolons:
init statement: which is executed before the first iteration.
condition expression: which is evaluated before every iteration.
post statement: which is executed at the end of every iteration.
Break and continue
As expected, Go also supports both break and continue statements for loop control. Let's try a quick example:
So, the continue statement is used when we want to skip the remaining portion of the loop, and break statement is used when we want to break out of the loop.
Also, Init and post statements are optional, hence we can make our for loop behave like a while loop as well.
funcmain(){i:=0for;i<10;{i+=1}}
Note: we can also remove the additional semi-colons to make it a little cleaner
Forever loop
Lastly, If we omit the loop condition, it loops forever, so an infinite loop can be compactly expressed. This is also known as the forever loop.
funcmain(){for{// do stuff here}}
This article is part of my open source Go Course available on Github.