For Loop in Go

18.01.2025 ยท dayanch

The for loop is the only loop construct in Go. It can be used in several ways, making it versatile for different scenarios.

Basic Syntax

The basic syntax of a for loop is:

for initialization; condition; post {
    // code to execute
}

Example 1: Counting from 1 to 5

for i := 1; i <= 5; i++ {
    fmt.Println(i)
}

This will print numbers from 1 to 5.

Example 2: Using a for loop with a slice

fruits := []string{"apple", "banana", "cherry"}
for i, fruit := range fruits {
    fmt.Printf("%d: %s\n", i, fruit)
}

This will print each fruit along with its index.

Example 3: Infinite Loop

You can create an infinite loop using for without any conditions:

for {
    // code to execute indefinitely
}

Be cautious with infinite loops, as they can cause your program to hang if not handled properly.

Conclusion

The for loop in Go is powerful and flexible, allowing you to iterate over collections, count, or even create infinite loops. Understanding its syntax and usage is crucial for effective programming in Go.

Share

Copyright 2023