Go Program to Find Sum of Slice Elements
In Go, you can find the sum of slice elements by looping through the slice with
for and adding each element to a total variable, like sum := 0; for _, v := range slice { sum += v }.Examples
Input[1, 2, 3]
Output6
Input[10, 20, 30, 40]
Output100
Input[]
Output0
How to Think About It
To find the sum of elements in a slice, think of adding each number one by one to a total. Start with zero, then go through each element in the slice, add it to the total, and finally return the total sum.
Algorithm
1
Start with a sum variable set to zero.2
Go through each element in the slice one by one.3
Add the current element's value to the sum.4
After all elements are added, return the sum.Code
go
package main import "fmt" func main() { numbers := []int{1, 2, 3, 4, 5} sum := 0 for _, value := range numbers { sum += value } fmt.Println(sum) }
Output
15
Dry Run
Let's trace the slice [1, 2, 3, 4, 5] through the code
1
Initialize sum
sum = 0
2
Add first element
sum = 0 + 1 = 1
3
Add second element
sum = 1 + 2 = 3
4
Add third element
sum = 3 + 3 = 6
5
Add fourth element
sum = 6 + 4 = 10
6
Add fifth element
sum = 10 + 5 = 15
7
Print sum
Output: 15
| Iteration | Current Element | Sum After Addition |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 3 | 3 | 6 |
| 4 | 4 | 10 |
| 5 | 5 | 15 |
Why This Works
Step 1: Initialize sum variable
We start with sum = 0 to have a base value to add numbers to.
Step 2: Loop through slice elements
Using for _, value := range slice, we access each element one by one.
Step 3: Add each element to sum
Inside the loop, we add the current element's value to sum using sum += value.
Step 4: Print the final sum
After the loop ends, sum holds the total, which we print.
Alternative Approaches
Using a function to sum slice
go
package main import "fmt" func sumSlice(nums []int) int { total := 0 for _, v := range nums { total += v } return total } func main() { numbers := []int{1, 2, 3, 4, 5} fmt.Println(sumSlice(numbers)) }
This approach separates logic into a reusable function, improving code clarity and reuse.
Using recursion to sum slice
go
package main import "fmt" func sumSlice(nums []int) int { if len(nums) == 0 { return 0 } return nums[0] + sumSlice(nums[1:]) } func main() { numbers := []int{1, 2, 3, 4, 5} fmt.Println(sumSlice(numbers)) }
Recursion is elegant but less efficient and can cause stack overflow for large slices.
Complexity: O(n) time, O(1) space
Time Complexity
The program loops through each element once, so time grows linearly with slice size, making it O(n).
Space Complexity
Only a few variables are used regardless of input size, so space complexity is constant O(1).
Which Approach is Fastest?
The simple loop approach is fastest and most memory efficient; recursion adds overhead and risk of stack overflow.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Simple loop | O(n) | O(1) | Most efficient and clear for all slice sizes |
| Function wrapper | O(n) | O(1) | Better code reuse and clarity |
| Recursion | O(n) | O(n) | Small slices or educational purposes |
Use a
for range loop to easily access each element in a slice for summing.Forgetting to initialize the sum variable to zero before adding elements causes incorrect results.