diff --git a/unit-testing/go.mod b/unit-testing/go.mod new file mode 100644 index 0000000..94f1634 --- /dev/null +++ b/unit-testing/go.mod @@ -0,0 +1,3 @@ +module unit-testing + +go 1.22.2 diff --git a/unit-testing/main.go b/unit-testing/main.go new file mode 100644 index 0000000..4a244d3 --- /dev/null +++ b/unit-testing/main.go @@ -0,0 +1,35 @@ +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) +}