课后题sumArray与sumSlice
func sumArray(arrF [4]int) int {
sum := 0
for _, it := range arrF {
sum += it
}
return sum
}
func sumSlice(arrF []int) int {
sum := 0
for _, it := range arrF {
sum += it
}
return sum
}
func sum(arrF ...int) int {
sum := 0
for _, it := range arrF {
sum += it
}
return sum
}
func minSlice(arrF []int) int {
n := arrF[0]
for _, it := range arrF {
if n > it{
n = it
}
}
return n
}
func main() {
fmt.Print(sumArray([4]int{1, 1, 1, 1}),"\n")
// ERROR
// cannot use [4]int{…} (value of type [4]int)
// as []int value in argument to sumSlice
// fmt.Print(sumSlice([4]int{1, 1, 1, 1}),"\n")
fmt.Print(sumSlice([]int{1, 2, 3, 4, 5}),"\n")
fmt.Print(sum(2, 2, 2, 2),"\n")
fmt.Print(minSlice([]int{1, 2, 3, 4, 5}),"\n")
}