I want to sum all the numbers in array using recursive function
import (
"fmt"
)
func RemoveIndex(s []int, index int) []int {
return append(s[:index], s[index+1:]...)
}
func recursiveSum(arr []int) int {
if len(arr) == 1 {
return arr[0]
}
sum := arr[0] + recursiveSum(RemoveIndex(arr, 0))
return sum
}
func main() {
var arr = []int{2, 4, 6}
fmt.Println(recursiveSum(arr))
}
I have such code. In fact there isn't any function for deleting element in array by index in Go, so I wrote function that did that. So, generaly I expected to get 12 and for some reason I get 18. Can please someone help and explain what exactly goes wrong here.