In Go, there is only one loop command, which is for. There are no while or do-while loops like other languages, but we can modify for to behave the same way.
When to use?
Use a Loop when you need to run the same block of code repeatedly, for example:
MAIN.GO
package mainimport "fmt"func main() { // 1. Standard Loop (Init; Condition; Post) // Most common for fixed counts. for i := 0; i < 5; i++ { fmt.Println("Number:", i) } // 2. While-style Loop (Condition only) // Used if the number of iterations is uncertain, depending on a condition. j := 0 for j < 3 { fmt.Println("While style:", j) j++ // Manual increment to avoid infinite loop }}