Skip to content

Go For Loops

Links: - 103 Golang Index
- Go - ProgramFlow


For Loops

  • There is only for loop in go.
  • continue and break statements have the same effect as in any other language.
    for i := 0; i<10; i++ {
        fmt.Println(i)
    }
    
  • Third statement is flexible in go
    // ; after i < 10 is important
    for i := 0; i<10; {
        fmt.Println(i)
        i++
    }
    
  • While loop using for
    i := 0
    for i < 10 {
        fmt.Println(i)
        i++
    }
    
  • Infinite loop
    for {
        // statements
    }
    for i := 0; true; i++ {
        // statements
    }
    
  • Handling multiple variables in a for loop
    for i, j := 0, 100; i < 10; i, j = i+1, j+1 {
        fmt.Printf("i = %v, j = %v\n", i, j)
    }
    
  • Iterating over an array
    for index, value := range someArray {
        // statements
    }
    // if you don't want index you can use _ instead of index
    

Last updated: 2022-05-21