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
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,
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,
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.
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,
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,
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,
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.
That's all 😃!
Top comments (0)