How to write a for loop in Golang or Go?
Tags: go
Published: 2022-08-13 19:04:40

---
  title: How to write a for loop in Golang or Go?
  published: true
  tags: go
  date: Sun Aug 14 2022 00:34:40 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/write-for-loop-in-golang-or-go
  cover_image: https://melvingeorge.me/_next/static/images/main-c38dcf9b68d0b675eff8019d2b8a26d6.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/write-for-loop-in-golang-or-go)
  
In Golang/Go, you will first use the `for` keyword followed by index variable initialization ending with the `;` symbol (semi-colon), then the loop condition ending with the `;` symbol, then the index variable incrementer/decrementer statement, and finally the `{}` symbol (curly brackets opening and closing) inside which the loop body is written.

### TL;DR

```golang
package main

import "fmt"

func main() {
    // a simple for loop that prints number from 1 to 10
    for i := 1; i <= 10; i = i + 1 {
        // cool code to execute on each loop iteration
        fmt.Println(i)
    }
}
```

For example, let's say we need a loop where we want to print the first 10 numbers.

To do that first, we can write the `for` keyword followed by the index variable initialization. Since we need it to start from the number `1` let's initialize the variable with the value of `1`. Let's call the index variable `i`.

It can be done like this,

```golang
package main

func main() {
    // a simple for loop that prints number from 1 to 10
    // NOTE: the below for loop is not complete and will result in error now
    for i := 1; {
        // cool code to execute on each loop iteration
    }
}
```

Now that we have written the index variable, let's write the loop condition after the index variable declaration where it will check on each iteration of the loop whether the condition is true and if only it is true it will execute the loop body.

In our case, we need this loop to run from value `1` to the value of `10`. So our loop condition becomes checking if the index variable `i` is less or equal to the value of `10` (`i <= 10`).

It can be done like this,

```golang
package main

func main() {
    // a simple for loop that prints number from 1 to 10
    // NOTE: the below for loop is not complete and will result in error now
    for i := 1; i <= 10; {
        // cool code to execute on each loop iteration
    }
}
```

Now after the loop condition we need to write the incrementer or the decrementer statement which increments or decrements the index variable value after each iteration in the loop. If we don't have an incrementer/decrementer the index variable value will be kept at `1` after each iteration in the loop and we will get something called an [Infinite Loop](https://en.wikipedia.org/wiki/Infinite_loop).

In our case, we need the index variable to keep on increasing by the value of `1` after each iteration in the loop. To do that we can add `1` to the current index variable value (`i = i + 1`, here we are reassigning the value of the index variable by adding 1 to the current value).

It can be done like this,

```golang
package main

func main() {
    // a simple for loop that prints number from 1 to 10
    for i := 1; i <= 10; i = i + 1 {
        // cool code to execute on each loop iteration
    }
}
```

For the final part we need to show the current value of the index variable on each iteration, to do that we can use the `Println()` method from the `fmt` module to print the index variable value to the console or terminal.

it can be done like this,

```golang
package main

import "fmt"

func main() {
    // a simple for loop that prints number from 1 to 10
    for i := 1; i <= 10; i = i + 1 {
        // cool code to execute on each loop iteration
        fmt.Println(i)
    }
}
```

Now if you run the above code you can see an output like this,

```bash
1
2
3
4
5
6
7
8
9
10
```

We have successfully written a `for` loop in Golang/Go. Yay 🥳!

See the above code live in [The Go Playground](https://go.dev/play/p/k2-Q2Pkoew9).

That's all 😃!

### Feel free to share if you found this helpful 😃.

---