Go – For loops

Reading Time: < 1 minute

Last Updated: 8/16/2024

import "fmt"

for i := 0; i < 5; i++ {
	fmt.Println(i)
}

Please note the case below where we are using a For loop. I buried the lead. There aren’t any stated starting and ending points. We take off just saying for and go to town. (p.s. See references programwiz which this code is almost directly taken from)

package main
import "fmt"

func main(){
  number := 1

  // loop that runs infinitely
  for {

    // condition to terminate the loop
    if number > 6 {
      break;
    }

    fmt.Printf("%d\n", number);
    number ++

  }
}

Reference:
https://www.geeksforgeeks.org/loops-in-go-language/
https://go.dev/tour/flowcontrol/1
https://go.dev/tour/flowcontrol/3
https://www.digitalocean.com/community/tutorials/how-to-construct-for-loops-in-go
https://www.programiz.com/golang/while-loop

This entry was posted in Go, Programming, Uncategorized. Bookmark the permalink.