golang-exercises/sum-args/main.go

31 lines
554 B
Go
Raw Permalink Normal View History

2024-02-16 18:26:12 +01:00
package main
import (
"fmt"
"os"
"strconv"
)
var result float64
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: ./sum-args 1 2 3 4 5 6")
os.Exit(1)
}
for i := 1; i < len(os.Args); i++ {
sum, _ := strconv.ParseFloat(os.Args[i], 64)
2024-02-16 18:26:12 +01:00
result = result + float64(sum)
}
averageResult := getAverage(result, len(os.Args)-1)
fmt.Printf("Total sum: %.4v\n", result)
fmt.Printf("Average: %.4v\n", averageResult)
}
func getAverage(num float64, quantity int) (finalNum float64) {
finalNum = num / float64(quantity)
return finalNum
2024-02-16 18:26:12 +01:00
}