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
  • using variadic Functions
  • 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 understanding of 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
    for _,
    v: = range array {
        result += v
    }
    return result
}
func main() {
    numb: = [] int {
        1, 2, 3, 4, 5
    }
    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 is declared with an array as a parameter, In this function, Used for with range from to iterate and calculate each element and assign to result.
This returns the result to the main function.
Finally, print the result to 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() {
    numb: = [] int {
        1, 2, 3, 4, 5
    }
    fmt.Print("Result :", addArray(numb...))
}
func addArray(numbs...int) int {
    result: = 0
    for _,
    numb: = range numbs {
        result += numb
    }
    return result
}

Output:

Result:15

In the above program,

Slice is created with initial values assignment

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 result variable, returned result to the function

Finally printed the result to the console using print

How to find 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]  
Sum of elements of 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.