Add unit-testing exercise

This commit is contained in:
raul 2024-05-10 08:57:55 +02:00
parent a6de42db31
commit b0e0884020
2 changed files with 38 additions and 0 deletions

3
unit-testing/go.mod Normal file
View File

@ -0,0 +1,3 @@
module unit-testing
go 1.22.2

35
unit-testing/main.go Normal file
View File

@ -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)
}