Loops in Go
Here is some common loops
Loop n times
1
2
3
4
5
|
n := 10
for i := 0; i < n ; i++ {
fmt.Println(i)
}
|
Loop through array
1
2
3
4
5
6
7
8
9
|
nums := []int{1, 2, 3, 4}
for i, v := range nums {
// index
fmt.Println(i)
// value
fmt.Println(v)
}
|
Loop with multiple indexes
1
2
3
|
for i, j := 0, 0; i < 10 || j < 12; i, j = i+1, j+1 {
// ...
}
|