Add unit-testing exercise
This commit is contained in:
parent
a6de42db31
commit
b0e0884020
|
@ -0,0 +1,3 @@
|
|||
module unit-testing
|
||||
|
||||
go 1.22.2
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue