Menu Close

Go – for loop in go golang

In this article, we are going to learn about for loop in go golang. Go has only one loop i.e for loop.

We can use for loop in multiple ways.

Basic for loop

Syntax:

 for init; condition; post statement {

    Statements 

 } 

In the above syntax:

  • init: It is short variable declaration and execute before the first iteration. The scope of this variable resides within the for loop statement.
  • condition: It is evaluate before every iteration. If the condition is true then statements within for loop will be executed otherwise it will exit from loop.
  • post statement:  It is executed at the end of every iteration.
package main

import "fmt"

func main() {

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

Output:

0
1
2
3
4

for loop as while loop

We can use for loop as while loop.

Syntax:

for condition {

    statements

}
package main

import "fmt"

func main() {

    i := 0

    for i < 5 {

        fmt.Println(i)
        i = i + 1
        
    }
}

Output:

 0
 1
 2
 3
 4

for loop as Infinite loop

we can use for loop as Infinite loop.

package main

import "fmt"

func main() {

    i := 0

    for {

        fmt.Println(i)
        i = i + 1
        
    }
}

Output:

0
1
2
3
4
...
... 

for-each range loop

We can use range in for loop.

Syntax:

 For index, element := range array/slice/map/channel {
     Statements
 } 
package main

import "fmt"

func main() {

    fruits := []string {"apple", "orange", "banana"}

    for index, fruit_name := range fruits {

      fmt.Printf("index: %d, fruit name: %s\n", index, fruit_name)

    }
}

Output:

index: 0, fruit name: apple
index: 1, fruit name: orange
index: 2, fruit name: banana

Example of for-each range loop with map :

package main

import "fmt"

func main() {

    fruits := map[string]int {"apple": 1, "orange": 2, "banana": 3}

    for index, fruit_name := range fruits {

      fmt.Println(index, fruit_name)

    }
}

Output:

orange 2
banana 3
apple 1

Example of for-each range loop with channel :

package main

import "fmt"

func main() {

    ch := make(chan string)

    go func() {
        ch <- "x"
        ch <- "y"
        ch <- "z"
        close(ch)
    }()

    for n := range ch {

        fmt.Println(n)

    }
}

Output:

x
y
z

nested for loop

We can also use nested for loop.

package main

import "fmt"

func main() {

    for i := 1; i <= 2; i++ {
    
        for j := 1; j <= 5; j++ {
        
            fmt.Println(i, "*", j, "=", i*j)
            
        }
    }
}

Output:

 1 * 1 = 1
 1 * 2 = 2
 1 * 3 = 3
 1 * 4 = 4
 1 * 5 = 5
 2 * 1 = 2
 2 * 2 = 4
 2 * 3 = 6
 2 * 4 = 8
 2 * 5 = 10

Exit from for loop

We can use either break or continue or goto statement to exit from for loop in go golang.

Example of continue statement:

package main

import "fmt"

func main() {

      for i := 0; i <= 10; i++ {

          if i% 2 != 0 {

              continue
          }

          fmt.Println(i)
      }
}

Output:

 0
 2
 4
 6
 8
 10

Example of break statement:

package main

import "fmt"

func main() {

      for i := 0; i <= 10; i++ {

          if i == 5 {

              break
          }

          fmt.Println(i)
      }
}

Output:

 0
 1
 2
 3
 4

To learn more about golang, Please refer given below link.

https://www.techieindoor.com/go-lang-tutorial/

References:

 https://golang.org/doc/
https://golang.org/pkg/
https://golang.org/pkg/fmt/
https://golang.org/pkg/fmt/#Println
Posted in golang

Leave a Reply

Your email address will not be published. Required fields are marked *