array_sum.go 410 B

123456789101112131415161718
  1. package main
  2. import "fmt"
  3. func main() {
  4. array := [3]float64{7.0, 8.5, 9.1}
  5. x := Sum(&array) // Note the explicit address-of operator to pass a pointer to the array
  6. fmt.Printf("The sum of the array is: %f", x)
  7. }
  8. func Sum(a *[3]float64) (sum float64) {
  9. for _, v := range a { // can also with dereferencing *a to get back to the array
  10. sum += v
  11. }
  12. return
  13. }
  14. // Output: The sum of the array is: 24.600000