OneBite.Dev - Coding blog in a bite size

do a while loop in Go

Code snippet on how to do a while loop in Go

  i := 0
  for i < 5 {
    fmt.Println("i is currently:", i)
    i++
  }

This is an example of a while loop in Go. The code starts by declaring a variable called “i” and setting it equal to 0. Next, there is a for loop that states that as long as “i” is less than 5, the following lines of code will be completed. Inside the for loop, it prints out a sentence that states what the value of “i” is and then it increments “i” by one at the end of each iteration. This will repeat until “i” is no longer less than 5. In this case, “i” will be printed out 5 times and will end with a value of 5.

go