mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 01:21:38 +08:00
update book code
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
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
|
||||
}
|
||||
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
|
||||
|
Reference in New Issue
Block a user