Files
the-way-to-go_ZH_CN/eBook/examples/chapter_7/array_sum.go
2013-11-20 14:50:00 +08:00

17 lines
395 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 { // derefencing *a to get back to the array is not necessary!
sum += v
}
return
}