Go Language: The For loop
One of the first things I heard about Go that sounded interesting is that it has only one way to create loops. Go only provides the for statement. This sounds a little weird, but the truth is that they just decided to use the for keyword for while loops instead of having a separate while keyword. Lets see an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main
import (
"fmt"
)
func main() {
people := []string{"Hugo", "Paco", "Luis"}
// Like a for
numPeople := len(people)
for i := 0; i < numPeople; i++ {
fmt.Println(people[i])
}
// Like a while
numPeople = len(people)
i := 0
for i < numPeople {
fmt.Println(people[i])
i++
}
}