add chapter 6
This commit is contained in:
20
chapter6/average/main.go
Normal file
20
chapter6/average/main.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user