Loops in Golang are control structures that allow repetitive execution of a block of code until a certain condition is met. They are used to iterate over collections, perform a specific task a certain number of times, or continuously execute code until a specific condition is satisfied.
In Golang, there are three types of loops:
- For loop: The
for
loop is the most commonly used loop in Golang. It allows you to repeatedly execute a block of code based on a condition or a fixed number of iterations. - While loop: Golang does not have a separate
while
loop construct. Instead, thefor
loop can be used as awhile
loop by omitting the initialization and post statements. - Infinite loop: An infinite loop is a loop that keeps executing indefinitely until an explicit break or return statement is encountered.
Loops allow us to efficiently execute a piece of code multiple times without having to duplicate it manually. They make our code more concise, readable, and maintainable. By using loops, we can handle large sets of data, perform computations, and implement various algorithms.
The for
loop is versatile and can be used in various scenarios. Here are a few examples:
for initialization; condition; post {
// code to be executed
}
initialization
initializes a counter variable or sets up the loop.condition
is evaluated before each iteration. If it's true, the loop continues; otherwise, it terminates.post
is executed after each iteration, usually used for incrementing or updating the counter.
numbers := []int{1, 2, 3, 4, 5}
for i := 0; i < len(numbers); i++ {
fmt.Println(numbers[i])
}
Output:
1
2
3
4
5
numbers := []int{1, 2, 3, 4, 5}
sum := 0
for _, num := range numbers {
sum += num
}
fmt.Println("Sum:", sum)
Output:
Sum: 15
// Print numbers from 1 to 5
for i := 1; i <= 5; i++ {
fmt.Println(i)
}
Output:
1
2
3
4
5
While Golang does not have a specific while
loop, you can achieve the same functionality using a for
loop with only a condition.
count := 10
for count > 0 {
fmt.Println(count)
count--
}
fmt.Println("Liftoff!")
Output:
10
9
8
7
6
5
4
3
2
1
Liftoff!
An infinite loop continues indefinitely until a break or return statement is encountered. It is often used when you want a program to keep running until a specific condition is met. If break or return statement is omitted, then the loop runs forever.
for {
var input string
fmt.Print("Enter a command: ")
fmt.Scanln(&input)
if input == "exit" {
break
}
fmt.Println("Executing command:", input)
}
Output:
Enter a command: run
Executing command: run
Enter a command: stop
Executing command: stop
Enter a command: exit
These examples demonstrate how loops can be used to solve various problems efficiently by repeating code execution or iterating over collections until a specific condition is met.
- Golang Tour - A Tour of Go.
- Golang Blog - The official blog of the Go programming language.
- Golang YouTube Channel - Official YouTube channel with videos covering various Go topics.
- Twitch - Live coding streams and discussions on Go programming.
Remember, loops are powerful tools that help us automate repetitive tasks and handle data efficiently. Understanding how to use loops effectively is crucial for writing clean and efficient code in Go.