• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Justin Joyce

Practical tips and tutorials about software development.

  • Standing Invitation
  • Featured Posts
  • Latest
  • About

Golang for loop

Posted May 12, 2023 — Updated Jan 10, 2024

Go has a few for loop options:

  1. Classic for: for i := 0; i < 5; i++
  2. for using range: for index, element := range listOfThings
  3. for as a while loop: for done != true

Classic for loop

This format is common in many languages:

func main() {
	for i := 0; i < 3; i++ {
    	fmt.Printf("Hi %d\n", i)
    }
}

main()
// Hi 0
// Hi 1
// Hi 2

The one difference in Go is the lack of opening parentheses around the init statements1, which are required in other languages like Javascript, Java, or C.

For loop using range

Golang doesn’t have a for in loop, but range acts similarly:

func main() {
	letterSlice := []string{"a", "b", "c"}
	for index, letter := range letterSlice {
		fmt.Printf("Index: %d, Letter: %s\n", index, letter)
	}
}

main()
// Index: 0, Letter: a
// Index: 1, Letter: b
// Index: 2, Letter: c

For loop as a while loop

Many languages have a for loop and a separate while loop—Go does not.

For is Go’s while:

func main() {
	complete := false
    for != complete {
    	// this will loop until you set complete = true inside the loop
    }
}

Break and continue statements

These work as expected with any of Go’s loop formats.

Continue skips to the next loop iteration:

func main() {
	for i := 0; i < 3; i++ {
    	if i == 1 {
        	fmt.Println("i == 1, skipping")
        	continue
        }
    	fmt.Printf("Hi %d\n", i)
    }
}

main()
// Hi 0
// i == 1, skipping
// Hi 2

Break exits the loop:

func main() {
	letterSlice := []string{"a", "b", "c"}
	for index, letter := range letterSlice {
		if letter == "c" {
        	fmt.Printf("found c, exiting loop")
			break
		}
		fmt.Printf("Index: %d, Letter: %s\n", index, letter)
	}
}

main()
// Index: 0, Letter: a
// Index: 1, Letter: b
// found c, exiting loop

If you have nested loops break and continue will only affect the innermost loop, but that’s common across all languages, not just Go.


  1. Technically it’s the init, condition, and post statements ↩︎

Filed Under: Golang

Primary Sidebar

Recent Posts

  • Every Built-In Vim Color Scheme (with screenshots)
  • Reverse a string in Python
  • Meeting Cost Calculator
  • Vim find and replace
  • What makes an effective development team

Categories

  • Arrays (5)
  • Command Line (9)
  • Dates (3)
  • Featured (7)
  • Git (7)
  • Golang (5)
  • Javascript (8)
  • Productivity (8)
  • Projects (4)
  • Python (15)
  • Regex (2)
  • Ruby (3)
  • Shell (2)
  • Thoughts (2)
  • Tips (11)
  • Tools (3)
  • Tutorials (1)
  • Vim (4)

Archives

  • July 2024 (1)
  • February 2024 (1)
  • January 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • October 2023 (4)
  • September 2023 (1)
  • August 2023 (2)
  • July 2023 (5)
  • June 2023 (3)
  • May 2023 (6)
  • April 2023 (5)
  • March 2023 (5)
  • February 2023 (10)
  • January 2023 (6)
  • December 2022 (7)

Copyright © 2025 · Contact me at justin [at] {this domain}

  • Privacy Policy