36 lines
488 B
Go
36 lines
488 B
Go
|
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)
|
||
|
}
|