made averageCalc variadic
This commit is contained in:
parent
805c0a7e21
commit
95241405da
26
playground/averageHeadFirstVariadic.go
Normal file
26
playground/averageHeadFirstVariadic.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func averageCalc(numbers ...float64) float64 {
|
||||||
|
var sum float64 = 0
|
||||||
|
for _, number := range numbers {
|
||||||
|
sum += number
|
||||||
|
}
|
||||||
|
return sum / float64(len(numbers))
|
||||||
|
}
|
||||||
|
|
||||||
|
func averageHeadFirstVariadic() {
|
||||||
|
arguments := os.Args[1:]
|
||||||
|
var numbers []float64
|
||||||
|
for _, argument := range arguments {
|
||||||
|
number, err := strconv.ParseFloat(argument, 64)
|
||||||
|
Error(err)
|
||||||
|
numbers = append(numbers, number)
|
||||||
|
}
|
||||||
|
fmt.Printf("Average: %0.2\n", averageCalc(numbers...))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user