Loops in Golang
In Go, loops are used to repeat a block of code multiple times until a certain condition is met. Go supports three types of loops: for
loop, while
loop (implemented using for
loop), and do-while
loop (implemented using for
loop with a break
statement).
- For Loop:
- The
for
loop in Go is the most commonly used loop and follows a similar structure to other programming languages. - It has three components: initialization, condition, and increment/decrement.
- The loop will continue as long as the condition is true.
- The
Syntax:
for initialization; condition; increment/decrement {
// Code to be repeated
}
Example :
package main
import "fmt"
func main() {
for i := 1; i <= 5; i++ {
fmt.Println("Iteration:", i)
}
}
Output :
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
- While Loop (Implemented using For Loop):
- Go does not have a specific
while
loop, but you can create awhile
loop behavior using thefor
loop with only a condition. - The loop will run as long as the condition is true.
- Go does not have a specific
Syntax:
for condition {
// Code to be repeated
}
Example :
package main
import "fmt"
func main() {
i := 1
for i <= 5 {
fmt.Println("Iteration:", i)
i++
}
}
Output :
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
- Do-While Loop (Implemented using For Loop with Break):
- Go does not have a direct
do-while
loop, but you can achieve its behavior using afor
loop with abreak
statement. - The loop will run at least once, and then it will continue as long as the condition is true.
- Go does not have a direct
Syntax:
for {
// Code to be repeated
if condition {
break
}
}
Example :
package main
import "fmt"
func main() {
i := 1
for {
fmt.Println("Iteration:", i)
i++
if i > 5 {
break
}
}
}
Output :
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
These loop constructs in Go allow you to efficiently execute repetitive tasks and control the flow of your program based on specific conditions. You can choose the appropriate loop type depending on the nature of your task and the desired behavior of the loop.
Range in Loops
In Go, the range
keyword is used in a for
loop to iterate over elements in various data structures such as arrays, slices, strings, maps, and channels. It simplifies the process of iterating over the elements of a collection without needing to use traditional indexing or explicit iteration variables.
The syntax for using range
in a for
loop is as follows:
for index, value := range collection {
// Code to be executed for each element (value) in the collection
}
Here’s how range
works with different data structures:
- Arrays and Slices:
- When used with arrays or slices,
range
returns the index and value of each element in the collection.
- When used with arrays or slices,
Example:
package main
import "fmt"
func main() {
numbers := []int{1, 2, 3, 4, 5}
for index, value := range numbers {
fmt.Printf("Index: %d, Value: %d\n", index, value)
}
}
Output :
Index: 0, Value: 1
Index: 1, Value: 2
Index: 2, Value: 3
Index: 3, Value: 4
Index: 4, Value: 5
- Strings:
- When used with a string,
range
iterates over each Unicode code point (character) in the string.
- When used with a string,
Example:
package main
import "fmt"
func main() {
message := "Hello, Go!"
for index, char := range message {
fmt.Printf("Index: %d, Character: %c\n", index, char)
}
}
Output :
Index: 0, Character: H
Index: 1, Character: e
Index: 2, Character: l
Index: 3, Character: l
Index: 4, Character: o
Index: 5, Character: ,
Index: 6, Character:
Index: 7, Character: G
Index: 8, Character: o
Index: 9, Character: !
- Maps:
- When used with maps,
range
iterates over key-value pairs, providing both the key and the corresponding value.
- When used with maps,
Example:
package main
import "fmt"
func main() {
ages := map[string]int{
"John": 30,
"Jane": 25,
"Tom": 35,
}
for name, age := range ages {
fmt.Printf("%s is %d years old\n", name, age)
}
}
Output :
John is 30 years old
Jane is 25 years old
Tom is 35 years old
Using range
in for
loops is a concise and idiomatic way to iterate over elements in collections, making your code more readable and maintainable. It simplifies the process of accessing elements and their corresponding indices or keys, providing an elegant solution for handling data structures.