Why does the go language have no min/max (int, int) function

Source: Internet
Author: User
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

  1. 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.
  2. 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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.