Control Flow in Go - Making Decisions and Loops Easy
An Introduction to Go's Decision and Loop Features for Beginners

Welcome back to the “Go Deep with Golang” series.
In the previous blogs, we covered the basics of Go, including variables, constants, and data types.
Now, it's time to explore one of the most important aspects of any programming language.
Control Flow
Control flow determines how your program makes decisions and repeats tasks. It helps decide what runs, when it runs, and how often it runs.
Let's explore all the control structures with their code snippets.
Conditional Statements in Go
The if statement
poackage main
import 'fmt'
func main() {
num := 10
if num > 0 {
fmt.Println("Positive number")
}
}
Short Statement in if
Go allows variable initialization directly inside if.
if num:= 10; num %2 == 0 {
fmt.Println("Even number")
} else {
fmt.Println("Odd numner")
}
The variable number is scoped only inside this if block.
Loops in GO
Go has only one loop, the for loop, but it's extremely flexible. You can use it as a traditional loop, a while-loop, or even create infinite loops.
Traditional for Loop
for i := 1; i <= 5; i++ {
fmt.Println(i)
}
Structure of this for loop code
initialisation → i := 1
Condition → i <= 5
Post statement → i++
While-like for Loop
You can skip initialisation and post expression.
i := 1
for i <= 5 {
fmt.Println(i)
i++
}
Infinite Loop
Omit all parts of for to create an infinite loop
for {
fmt.Println("Running..")
break
}
Switch Statement
day := 3
switch day {
case 1:
fmt.Println("Monday")
case 2:
fmt.Println("Tuesday")
case 3:
fmt.Println("Wednesday")
default:
fmt.Println("Invalid day")
}
Unique Features of switch in Go
No break needed - Go automatically breaks after each case.
Multiple values per case:
day :=3 switch day { case 1,2,3,4,5: fmt.Println("Weekday") case 6,7: fmt.Println("Weekend") }
Expressionless switch - act like a chain of if-else:
num := 42 switch { case num < 0: fmt.Println("Negative") case num == 0: fmt.Println("Zero") default: fmt.Println("Positive") }fallthrough keyword - forces the execution of the next case:
switch num := 2; num { case 1: fmt.Println("One") case 2: fmt.Println("Two") fallthrough case 3: fmt.Println("Three") default: fmt.Println("Other") } // Output Two Three
Goto Statement
This allows you to jump to a labeled statement. It’s rarely used but can help in breaking out of nested loops.
for i := 1; i <= 3; i++ {
for j := 1; j <= 3; j++ {
if i*j > 3 {
goto end
}
fmt.Println(i, j)
}
}
end:
fmt.Println("Loop ended")
// output
1 1
1 2
Loop ended
Conclusion
Control flow in Go is designed to be simple, readable, and minimalistic, yet powerful enough to handle all logical branching and looping needs. With just a few keywords—if, for, and switch—Go provides all the flexibility most programs need.
Understanding these concepts builds a strong foundation for writing clean, predictable, and efficient Go code.




