mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 04:48:29 +08:00
update book code
This commit is contained in:
39
eBook/exercises/chapter_7/sum_array.go
Executable file
39
eBook/exercises/chapter_7/sum_array.go
Executable file
@@ -0,0 +1,39 @@
|
||||
// leaving out the length 4 in [4] uses slice instead of an array
|
||||
// which is generally better for performance
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// var a = [4]float32 {1.0,2.0,3.0,4.0}
|
||||
var a = []float32 {1.0,2.0,3.0,4.0}
|
||||
fmt.Printf("The sum of the array is: %f\n", Sum(a))
|
||||
var b = []int {1, 2, 3, 4, 5}
|
||||
sum, average := SumAndAverage(b)
|
||||
fmt.Printf("The sum of the array is: %d, and the average is: %f", sum, average)
|
||||
}
|
||||
|
||||
/*
|
||||
func Sum(a [4]float32) (sum float32) {
|
||||
for _, item := range a {
|
||||
sum += item
|
||||
}
|
||||
return
|
||||
}
|
||||
*/
|
||||
|
||||
func Sum(a []float32) (sum float32) {
|
||||
for _, item := range a {
|
||||
sum += item
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func SumAndAverage (a []int) (int, float32) {
|
||||
sum := 0
|
||||
for _, item := range a {
|
||||
sum += item
|
||||
}
|
||||
return sum, float32(sum/len(a))
|
||||
}
|
||||
|
Reference in New Issue
Block a user