This is a created article in which the information may have evolved or changed.
Golang why there is no Min/max (int, int) function
We know that the Go language math package defines the Min/max function, but it is of the float64 type and does not have an integer type of Min/max.
The Min/max function defined in the Go language math package is as follows:
math.Min(float64, float64) float64math.Max(float64, float64) float64
In fact, we are more often compared to a two-integer scenario:
math.Min/Max(int, int), ormath.Min/Max(int64, int64)
So why does the go language not provide the integer type of Min/max these two functions? The following article gives an explanation:
Https://mrekucci.blogspot.jp/2015/07/dont-abuse-mathmax-mathmin.html
To sum up, the main reason is that
- Since the float64 type handles infinity and Not-a-number, and their processing is complex and the average user is not capable, all go needs to provide a system-level solution to the user.
- For int/int64 types of data, the implementation of Min/max is straightforward, and the user can fully implement it himself, for example:
func Min(x, y int64) int64 { if x < y { return x } return y}
The conclusion is that go wants users to implement such simple functions themselves.
So my question is:
Because the use of Min/max (int, int) is so common, does it require users to implement a Min/max code within each of their own projects, and why not put them in the system library such as math, for everyone to use?