Go Fundamentals - Sample
Table of Contents
Chapter 3.3: Iteration
The "for" Loop
In Go there is only one looping construct; the for
loop. The for
loop is very versatile and can be used to implement patterns such as: for
, while
, do while
, and do until
.
for i := 0; i < N; i++ {
// do work until i equals N
}
for
loop in Go.Iterating Over Arrays and Slices
Iterating over arrays, slices, and maps are done using the for
loop. In Listing 3.2 the len
function is used to return the length of the array, 4
, so the for
loop will stop when i
reaches 4
.
func main() {
names := [4]string{"Kurt", "Janis", "Jimi", "Amy"}
// iterate over the array using a for loop
for i := 0; i < len(names); i++ {
fmt.Println(names[i])
}
}
$ go run .
Kurt
Janis
Jimi
Amy
--------------------------------------------------------------------------------
Go Version: go1.23.0
The "range" Keyword
Previously we used a "classic" for
loop for iteration. Looping over collection types is common in Go that the language offers the range
keyword to simplify this code.
names := [4]string{"Kurt", "Janis", "Jimi", "Amy"}
for i, n := range names {
fmt.Printf("%d %s\n", i, n)
}
$ go run .
0 Kurt
1 Janis
2 Jimi
3 Amy
--------------------------------------------------------------------------------
Go Version: go1.23.0
range
keyword to iterate over an array.Range returns the index and the value of each item in the array or slice. If only the index of the loop is needed, and not the value, using only a single variable in the for
loop with range
will return the index of each loop.
func main() {
names := [4]string{"Kurt", "Janis", "Jimi", "Amy"}
for i := range names {
fmt.Printf("%d %s\n", i, names[i])
}
}
$ go run .
0 Kurt
1 Janis
2 Jimi
3 Amy
--------------------------------------------------------------------------------
Go Version: go1.23.0
range
keyword to iterate over an array, only returning the index.A lot of languages expose an interface, or similar mechanism, that can be implemented to allow for custom iterable types. Go does not provide any such interface. Only the built-in collection types, and a few other built-in types to be discussed later, are supported with the range
keyword.
Controlling Loops
The continue
keyword allows us to go back to the start of the loop and stop executing the rest of the code in the for
block.
if i == 3 {
// go to the start of the loop
continue
}
continue
keyword.This does not stop the loop from executing, but rather ends that particular run of the loop.
To stop execution of a loop we can use the break
keyword.
if i == 10 {
// stops the loop
break
}
break
keyword.Using the continue
and break
keywords we can control a for
loops execution to deliver the results we want.
func main() {
// create a variable to act as an index
var i int
// create an infinite loop
for {
// increment the index
i++
if i == 3 {
// go to the start of the loop
continue
}
if i == 10 {
// stops the loop
break
}
fmt.Println(i)
}
fmt.Println("finished")
}
$ go run .
1
2
4
5
6
7
8
9
finished
--------------------------------------------------------------------------------
Go Version: go1.23.0
continue
and break
keywords.Do While Loop
A do while
loop is used in a situation where you want the loop to run at least 1
iteration, regardless of the condition
.
A C/Java-style example would look something like Listing 3.8.
do {
// increment the index
i++;
// do the task
task();
// while the index is
// less than N, continue
} while (i < N);
do while
loop.To create a do while
style loop in Go a combination of an infinite loop and the break
keyword can be used as in Listing 3.9.
// declare an index variable (0)
var i int
// use an infinite loop
// this ensures the first
// iteration is always executed
for {
// increment the index
i++
// do the task
task()
// while the index is
// less than N, continue
if i < N {
// index is N or greater
// break the loop
break
}
}
do while
loop in Go.