array_sum.go 395 B

12345678910111213141516
  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
  6. // to pass a pointer to the array
  7. fmt.Printf("The sum of the array is: %f", x)
  8. }
  9. func Sum(a *[3]float64) (sum float64) {
  10. for _, v := range a { // derefencing *a to get back to the array is not necessary!
  11. sum += v
  12. }
  13. return
  14. }