Multiple ways to sum the elements of an array/slice in Golang example

This post covers some of the elements of an array/slice using the below approaches in the go language.

  • sum array elements using for range form loop
  • using variadic Functions
  • for loop to find the sum of an array of elements
  • Sum of elements entered by user input

In golang, a group of elements is stored in an array or slice type.

To understand the below programs, You have the understand the following features in Go Language.

Example program Sum of array elements using for loop range

In this example, We will use for loop to iterate elements in an array or slice

package main

import (
    "fmt"
)

func sum(array[] int) int {
    result: = 0
// Iterate each element, add an element  to a variable
    for _,v: = range array {
        result += v
    }
    return result
}
func main() {
    // Declare an array of 5 elements
    numb: = [] int {
        1, 2, 3, 4, 5
    }
// Sum function is called
    fmt.Print("Result :", sum(numb))
}

Output

Result:15

In the above program, the slice is declared and created with initial values using the shorthand assignment operator.

The function sum is declared with an array as a parameter, In this function, Use for with range to iterate and add each element and assign it to the result.
This returns the result to the main function.
Finally, print the result to the console using the Print function.

How to calculate Sum of array/slice elements using a variadic function

variadic functions are functions with variable arguments represented by … syntax The function can have multiple arguments.

package main

import (
    "fmt"
)

func main() {
    // slice declaration
    numb: = [] int {
        1, 2, 3, 4, 5
    }
    fmt.Print("Result :", addArray(numb...))
}
func addArray(numbs...int) int {
    result: = 0
    // iterate each element, add an element to the variable declaration
    for _,
    numb: = range numbs {
        result += numb
    }
    return result
}

Output:

Result:15

In the above program,

Slice is created with the assignment of the initial value

A function is declared with variable arguments, these are called a variadic function.

Using for loop range type, Iterated, and the sum of each iterated element, assign the result to the result variable, return the result to the function.

Finally, the result is to the console using print.

Sum of array elements using for loop

Basic for loop used to iterate an array,

In this example, for loop started with index=0, iterates an element, adds to a variable, and returns the result.

package main

import (
    "fmt"
)

func sum(array []int) int {
    result := 0
    // Iterate each element, add an element  to a variable
    for i := 0; i < len(array); i++ {
        result += array[i]
    }
    return result
}
func main() {
    // Declare an array of 5 elements
    numb := []int{1, 2, 3, 4, 5}
    // Sum function is called
    fmt.Print("Result :", sum(numb))
}

How to find the Sum of array elements input entered by the user in golang?

In this example, an Array of elements are entered by the user from the console.

package main

import (
    "fmt"
)

func main() {
    size: = 0
    fmt.Print("Number of elements n=")
    fmt.Scanln( & size)
    fmt.Println("Enter of array elements")
    elements: = make([] int, size)
    for i: = 0;i < size;i++{
        fmt.Scanln( & elements[i])
    }
    fmt.Println("Entered Array of elements:", elements)
    result: = 0

        for i: = 0;i < size;i++{
        result += elements[i]

    }
    fmt.Println("Sum of elements of array:", result)
}

Output:

Number of elements n=5  
Enter of array elements  
1  
3  
6  
2  
8  
Entered Array of elements: [1 3 6 2 8]  
The sum of elements of an array: 20

Here the number of elements are entered by user input and stored in the size variable.

Created a slice or array using the make function

Take the number of array elements from the user and store them in the array under variable elements.

Finally, the sum of array elements using for loop with range keyword, print to console.

Conclusion

In this example program, You learned multiple ways to sum the array or slice of elements.