Learn coding concepts by building real projects with modern technologies

All the Ways to Write for Loops in Go

By Lane Wagner on Apr 10, 2021

A for loop executes a block of code repeatedly, and in Golang, there are several different ways to write one.

#1 The standard three-component loop

Go has fairly standard syntax for the three-component loop you’re used to from C, Java, or JavaScript. The big difference is the lack of parentheses surrounding the components.

for i := 0; i < 100; i++ {
    sum += i

The three components are:

The compiler executes the for-loop in the following manner:

  1. The init statement executes and variables declared there are made available to the scope of the loop’s body.
  2. The condition is computed. If it evaluates to true then the body runs, otherwise the loop is complete.
  3. The post statement runs.
  4. Back to step #2

#2 For-range loop

More often than not, you’ll be looping over a collection of items like a map, slice, channel, or string. While you can use a traditional three-component loop, Go makes it easier by providing the range keyword.

Range over a slice in Go

fruits := []string{"apple", "banana", "pear"}
for i, fruit := range fruits {
    fmt.Println(i, s)

// prints:
// 0 apple
// 1 banana
// 2 pear

Range over a map in Go

ages := map[string]int{
    "lane":    26,
    "preston": 28,
    "rory":    21,
for name, age := range ages {
    fmt.Println(name, age)

// prints:
// lane 26
// preston 28
// rory 21

Range over a channel in Go

ch := make(chan int)
go func() {
    for i := 0; i < 3; i++ {
        ch <- i

// loop ends when channel is close
for value := range ch {
fmt.Println("channel closed")

// prints:
// 0
// 1
// 2
// channel closed

Range over a string in Go

name := "lane"
for i, char := range name {
     // cast the rune to a string for printing 
     fmt.Println(i, string(char))

// prints
// 0 l
// 1 a
// 2 n
// 3 e

#3 While loop

By using one component in a for-loop signature rather than three, we can effectively build a while-loop in Golang. There is no while keyword in Go.

sum := 1
for sum < 10 {
    sum += sum

Learn Go by writing Go code

I'm a senior engineer learning Go, and the pace of Boot.dev's Go Mastery courses has been perfect for me. The diverse community in Discord makes the weekly workshops a blast, and other members are quick to help out with detailed answers and explanations.

- Daniel Gerep from Cassia, Brasil

#4 Optional components loop

Building on the idea of a flexible for-loop, we can omit the init or post statements of the three-component loop as we please.

i := 0
for ; sum < 1000; i++ {
    sum += i

for i := 0; sum < 1000; {
    sum += i

This can be a useful pattern when you want something like a do-while, or an immediate first tick from a ticker.

#5 Infinite loop

Infinite loops are useful within goroutines when you have a worker or process that should continue perpetually.

sum := 0
for {
    sum++ // repeated forever
// never reached, loops continues on forever

Learn JavaScript by building real projects

I was a field service engineer and I wanted to learn to code, but work and family limited my options. When I found Boot.dev, the "Intro to Coding in JavaScript" course got me up and running immediately, and I knew I made the right decision as soon as I joined the Discord community.

- Özgür Yildirim from Germany

#6 Break from a loop

Breaking early from a loop can be useful, especially in a forever loop. The break keyword will exit the loop immediately.

sum := 0
for {
    if sum >= 1000 {

// prints:
// 1000

#7 Continue (skip to the next iteration) in a loop

It can be useful to skip to the next iteration of a loop early. This can be a good pattern for guard clauses within a loop.

for i := 0; i < 10; i++{
    if i % 2 == 0 {
    fmt.Println(i, "is odd")

// prints
// 1 is odd
// 3 is odd
// 5 is odd
// 7 is odd
// 9 is odd

Learn to code by building real projects

Related Reading