DEV Community

petercour
petercour

Posted on

No max/min function for integer in GoLang

#go

There is no min/max function in the core golang module. Why? Because floating point numbers are not easy to compare.

To avoid trouble, the min/max functions are provided in the GoLang math package as built in functions.

math.Min(float64, float64) float64
math.Max(float64, float64) float64

So that is for floating points. What if you want to compare integers? Then you have to write the functions yourself,

func Min(x, y int64) int64 {
 if x < y {
   return x
 }    
 return y
}

func Max(x, y int64) int64 {
 if x > y {
   return x
 }
 return y
}

More on golang: https://golangr.com

Top comments (0)