Hi there!

In this programming, we will learn how to use loops in golang.

For Loop

The for loop allows you to perform a certain action as many times as long as the condition returns true.

package main

import (
    "fmt"
)

func main() {
    for i := 0; i < 6; i++ {
        fmt.Println(i)
    }
}

The loop starts with the word for keyword, then the parameter i is initialized, then the condition when the loop should stop. In this example, the loop will work until i is less than 6. And at the end, we specify that we want to increase i by 1 after each iteration. Well, in the body of the loop, I simply display the value of i.

$ go run main.go
0
1
2
3
4
5

The i parameter is available only in the body of the loop. If I try to use it outside I get an error

$ go run main.go
# command-line-arguments
./main.go:11:14: undefined: i

For i to be used outside the loop, its initialization must be done outside the loop.

package main

import (
    "fmt"
)

func main() {
    i := 0
    for ; i < 6; i++ {
        fmt.Println(i)
    }
    fmt.Printf("Final I is %v\n", i)
}
$ go run main.go
0
1
2
3
4
5
Final I is 6

; must remain, otherwise, the next error will occur

$ go run main.go
# command-line-arguments
./main.go:9:17: syntax error: unexpected {, expecting semicolon or newline
./main.go:10:17: syntax error: unexpected newline, expecting { after for clause

The part where we increment i using i++ can also be moved to the body of the loop. In this case ; can be removed.

package main

import (
    "fmt"
)

func main() {
    i := 0
    for i < 6 {
        i++
        fmt.Println(i)
    }
    fmt.Printf("Final I is %v\n", i)
}
$ go run main.go
1
2
3
4
5
6
Final I is 6

Also, the increment of i does not necessarily have to be in the form of i++. You can perform other operations. For example, multiplication.

package main

import (
    "fmt"
)

func main() {
    for i := 0; i < 10; i = i + 1*2 {
        fmt.Println(i)
    }
}
$ go run main.go
0
2
4
6
8

Break and Continue

Continue

Continue can be used if in the loop we want to skip some iteration under a certain condition. In the following example, we check whether i is an even number, if so, we do not continue this iteration and go to the next one

package main

import (
    "fmt"
)

func main() {
    for i := 0; i < 10; i++ {
        if i%2 == 0 {
            continue
        }
        fmt.Println(i)
    }
}
$ go run main.go
1
3
5
7
9

Break

The break is used if you need to end the loop under a certain condition. In this example, the loop works as long as i is less than 10. But in the body of the loop, there is a check that if i is equal to 5, then the loop ends immediately

package main

import (
    "fmt"
)

func main() {
    for i := 0; i < 10; i++ {
        if i == 5 {
            break
        }
        fmt.Println(i)
    }
}
go run main.go
0
1
2
3
4

Range

Loops are also used to iterate over elements in data types such as an array, slice, map, and others. This is done using the range keyword. And has the form for INDEX, VALUE := range SLICE {CODE}.

Range in array/slice

package main

import (
    "fmt"
)

func main() {
    a := []int{1, 5, 7, 9, 10}

    for i, v := range a {
        fmt.Println(i, v)
    }
}
$ go run main.go
0 1
1 5
2 7
3 9
4 10

In this example, the index of the element in the slice and the element itself are displayed. If we don’t need an index, it should be replaced with _

package main

import (
    "fmt"
)

func main() {
    a := []int{1, 5, 7, 9, 10}

    for _, v := range a {
        fmt.Println(v)
    }
}
$ go run main.go
1
5
7
9
10

If we leave the index parameter in the loop but do not use it in the body of the loop, we will get an error

$ go run main.go
# command-line-arguments
./main.go:10:6: i declared but not used

That is why it needs to be replaced with _.

Range in maps

The range loop for the map looks the same as for the slice, with the only difference that there will be a key value instead of an index

package main

import (
    "fmt"
)

func main() {
    a := map[string]int{"A": 1, "B": 5, "C": 7, "D": 9, "E": 10}

    for k, v := range a {
        fmt.Println(k, v)
    }
}
$ go run main.go
D 9
E 10
A 1
B 5
C 7

Video