golang-exercises/unit-testing/main.go

36 lines
488 B
Go
Raw Normal View History

2024-05-10 08:57:55 +02:00
package main
import (
"fmt"
"log"
"math"
"os"
"strconv"
)
type Numbers interface {
int64 | float64 | int
}
func main() {
fmt.Print()
if len(os.Args) != 2 {
fmt.Println("Usage: ./sqrt 49")
os.Exit(1)
}
arg, err := strconv.Atoi(os.Args[1])
if err != nil {
log.Fatalf("Error occurred calculating square root: %v\n", err)
}
s := getSquareRoot(arg)
fmt.Println(s)
}
func getSquareRoot[T Numbers](n T) T {
squareRoot := math.Sqrt(float64(n))
return T(squareRoot)
}