add chapter 6

This commit is contained in:
Archie Fox
2025-02-04 12:01:07 +03:00
parent af9bb46af0
commit 31318a75ac
5 changed files with 105 additions and 0 deletions

20
chapter6/average/main.go Normal file
View File

@@ -0,0 +1,20 @@
package main
import (
"fmt"
"headfirstgo/headfirstgo/chapter6/datafile"
"log"
)
func main() {
numbers, err := datafile.GetFloats("data.txt")
if err != nil {
log.Fatal(err)
}
var sum float64 = 0
for _, number := range numbers {
sum += number
}
sampleCount := float64(len(numbers))
fmt.Printf("Average: %.2f\n", sum/sampleCount)
}