OneBite.Dev - Coding blog in a bite size

do a for loop in Go

Code snippet on how to do a for loop in Go

  for i := 0; i < 10; i++ {
    fmt.Println("Loop number:", i)
  }

This code is an example of a for loop in the Go programming language. It will print a statement (“Loop number:”) 10 times, starting with “Loop number: 0” and ending with “Loop number: 9”. The first line of the loop (“for i := 0”) sets the initial value of the counter variable (i). The second line of the loop (“i < 10”) checks to see if the counter variable is less than 10. If it is, the loop will execute the code within the curly brackets. The last line of the loop (“i++”) increments the counter variable by 1. The loop will then check the condition to see if i is still less than 10, and the process will repeat until it is no longer true.

go