Flow Control
5 min read
- Authors
- Name
- Karan Pratap Singh
- @karan_6864
Table of Contents
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 ()
.
func main() {
x := 10
if x > 5 {
fmt.Println("x is gt 5")
} else if x > 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.
func main() {
if x := 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:
func main() {
day := "monday"
switch day {
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.
switch day := "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.
switch day := "monday"; day {
case "monday":
fmt.Println("time to work!")
fallthrough
case "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 := 10
switch {
case x > 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.
func main() {
for i := 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:
func main() {
for i := 0; i < 10; i++ {
if i < 2 {
continue
}
fmt.Println(i)
if i > 5 {
break
}
}
fmt.Println("We broke out!")
}
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.
func main() {
i := 0
for ;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.
func main() {
for {
// do stuff here
}
}