Looping with Golang’s Arrays and Slices

We’ve so far discussed how you can declare and initialize arrays and slices in Golang. In this blog post, we discuss how you can use loops to initialize your arrays and slices.

Why do we need loops when initializing some arrays and slices?

Well, there may be a situation, when you need to initialize your array using a block of several values in some times. In such a scenario, you need to use Golang loops to simplify your code. Here’s an example of initializing an array in Golang where:

var balance = [10]float64{1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.01000.0, 1000.0}

In the above example, an array named balance has been initialized with 10 data elements of float64 type. Such a code can be simplified using loops. A loop statement enables you to execute a program statement or group of program statements multiple times. In the above example, a single loop can be written to initialize the balance ten times since the number of elements are ten.

Structure of Golang Loops
The Golang has for loop that is a repetition control structure that allows programmers to write efficiently loops that execute multiple times while initializing Golang’s arrays and slices. Here’s the syntax for specifying for loop in Golang:

for [condition |  ( init; condition; increment ) | range]
{
   Golang statement(s);
}

Here’s what you should know about this syntax:
If the condition is available, then the loop will execute as long as the condition remains true. If the for clause that is (init; condition; increment) here’s how the loop will be executed:

    • • The init step will be executed first, and only once. You can declare and initialize any of your loop control variables. Program statements are placed in this section as long as the semicolon exists.

 

    • • Next, the condition will be evaluated. If the condition is true, then the body of for loop will be executed. On the other hand, if the condition is false, the body of the loop will not run and flow of the program will jump to the next program statement just after the loop.

 

    • • After the body of for loop has executed the flow of program will jump back up to the increment statement.

 

    • • The condition of your loop will now be evaluated again. If it’s true, then the loop will execute, and the process repeats itself until the condition becomes false, after which for loop is terminated.

 

    • If the range is specified, then for loop will execute for each data item that’s in the range.

Examples of looping with arrays and slices in Golang
Here’s an example of how you can use loops for your arrays in Golang:

func main () {
var a [5] float64
a [0] = 100
a [1] = 200
a [2] = 300
a [3] = 350
a [4] = 500
var sum float64 = 0
for count := 0; count < 5; count++ {
sum += a[count]
}
fmt.Println(sum / 5)
}

The above Golang program calculates the average of 5 numbers which have been implemented in arrays. Notice how for loop has been used to compute the sum.

Next up, let’s have a look at how you can use for loops to implement Golang slices:

func main () {
var a =[5] float64 {100, 200, 300, 350, 500}
var sum float64 = 0
for count := 0; count < 5; count++ {
sum += a[count]
}
fmt.Println (“The sum of 5 data elements in the slice is:”, sum / 5)
}

There you have it—you can now begin using loops to implement Golang’s arrays and slices.

GET YOUR FREE Golang EBOOK!

(Visited 1,190 times, 1 visits today)