From f68fdf3d9b44fe9fda8b9cd59325368b29eb3b3b Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Sat, 3 Feb 2024 11:15:06 -0500 Subject: [PATCH] fixdom random errors and bugs --- cmd/headFirstCodeMagnets/variadic.go | 2 +- cmd/playground/averageHeadFirstVariadic.go | 4 ++-- cmd/playground/variadic.go | 8 ++++---- cmd/sum/sum.go | 5 ++++- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/cmd/headFirstCodeMagnets/variadic.go b/cmd/headFirstCodeMagnets/variadic.go index 3ad8f88..134f60a 100644 --- a/cmd/headFirstCodeMagnets/variadic.go +++ b/cmd/headFirstCodeMagnets/variadic.go @@ -3,7 +3,7 @@ package main import "fmt" func sum(numbers ...int) int { - var sum int = 0 + var sum = 0 for _, number := range numbers { sum += number diff --git a/cmd/playground/averageHeadFirstVariadic.go b/cmd/playground/averageHeadFirstVariadic.go index b68ce90..f8d9a28 100644 --- a/cmd/playground/averageHeadFirstVariadic.go +++ b/cmd/playground/averageHeadFirstVariadic.go @@ -14,7 +14,7 @@ func averageCalc(numbers ...float64) float64 { return sum / float64(len(numbers)) } -func averageHeadFirstVariadic() { +func main() { arguments := os.Args[1:] var numbers []float64 for _, argument := range arguments { @@ -22,5 +22,5 @@ func averageHeadFirstVariadic() { Error(err) numbers = append(numbers, number) } - fmt.Printf("Average: %0.2\n", averageCalc(numbers...)) + fmt.Printf("Average: %0.2f\n", averageCalc(numbers...)) } diff --git a/cmd/playground/variadic.go b/cmd/playground/variadic.go index 58cc2d2..5f4b2d1 100644 --- a/cmd/playground/variadic.go +++ b/cmd/playground/variadic.go @@ -6,13 +6,13 @@ import ( ) func maximum(numbers ...float64) float64 { - max := math.Inf(-1) + maxNumber := math.Inf(-1) for _, number := range numbers { - if number > max { - max = number + if number > maxNumber { + maxNumber = number } } - return max + return maxNumber } func inRange(min float64, max float64, numbers ...float64) []float64 { diff --git a/cmd/sum/sum.go b/cmd/sum/sum.go index 6bb43ef..ae2ac41 100644 --- a/cmd/sum/sum.go +++ b/cmd/sum/sum.go @@ -15,7 +15,10 @@ func OpenFile(fileName string) (*os.File, error) { func CloseFile(file *os.File) { fmt.Println("Closing file") - file.Close() + err := file.Close() + if err != nil { + log.Fatal(err) + } } func GetFloats(fileName string) ([]float64, error) {