Menu Close

Go – How to use while loop in go

Here, We will see how to use while loop in go . There is no such while loop in go. There is only one loop i.e for loop in go. We can use for loop as while loop in go.

Syntax:

for bool-condition { 

  // statements 

}

Example with code:

package main
 
import "fmt"
 
func main() {
 
    i := 0
 
    for i < 5 {
 
        fmt.Println(i)

        i = i + 1
         
    }
}

Output:

0
1
2
3
4

Example with code:

package main

import "fmt"

func main() {

    i := 0

    for true {

        fmt.Println(i)

        if i > 5 {

          break

        }

        i = i + 1

    }
}

Output:

0
1
2
3
4
5
6

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/
Posted in golang, Miscellaneous, os

Leave a Reply

Your email address will not be published.

Contact Us