Files
the-way-to-go_ZH_CN/eBook/examples/chapter_7/array_sum.go
2015-03-03 12:25:25 -05:00

18 lines
428 B
Go

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