trying_out_go/playground/averageHeadFirstSolution.go

25 lines
525 B
Go

package main
import (
"fmt"
"strings"
"git.linuxhg.com/Go_Training/datafile"
"git.linuxhg.com/Go_Training/keyboard"
)
func averageHeadFirstSolution() {
fmt.Printf("What is the name of the file: ")
fileString, err := keyboard.GetString()
Error(err)
fileString = strings.TrimSpace(fileString)
numbers, err := datafile.GetFloats(fileString)
Error(err)
var sum float64 = 0
for _, number := range numbers {
sum += number
}
sampleCount := float64(len(numbers))
fmt.Printf("Average: %0.2f\n", sum/sampleCount)
}