Find the minimum value in golang?

huangapple go评论90阅读模式
英文:

Find the minimum value in golang?

问题

在Go语言中,有一个最小值函数math.Min(https://golang.org/pkg/math/#Min)。但是如果我有多于两个数字怎么办?我必须在for循环中手动比较,还是有其他方法?这些数字存储在切片中。

英文:

In the language there is a minimum function https://golang.org/pkg/math/#Min But what if I have more than 2 numbers? I must to write a manual comparison in a for loop, or is there another way? The numbers are in the slice.

答案1

得分: 16

不,没有比循环更好的方法。循环不仅比其他方法更清晰,而且速度也更快。

values := []int{4, 20, 0, -11, -10}

min := values[0]
for _, v := range values {
		if (v < min) {
    		min = v
		}
}

fmt.Println(min)

编辑

由于评论中有关错误处理和如何处理空切片的讨论,这里是一个确定最小值的基本函数。记得导入errors

func Min(values []int) (min int, e error) {
    if len(values) == 0 {
        return 0, errors.New("无法在空切片中找到最小值")
    }
    
    min = values[0]
    for _, v := range values {
            if (v < min) {
                min = v
            }
    }
    
    return min, nil
}
英文:

No, there isn't any better way than looping. Not only is it cleaner than any other approach, it's also the fastest.

values := []int{4, 20, 0, -11, -10}

min := values[0]
for _, v := range values {
		if (v &lt; min) {
    		min = v
		}
}

fmt.Println(min)

EDIT

Since there has been some discussion in the comments about error handling and how to handle empty slices, here is a basic function that determines the minimum value. Remember to import errors.

func Min(values []int) (min int, e error) {
    if len(values) == 0 {
        return 0, errors.New(&quot;Cannot detect a minimum value in an empty slice&quot;)
    }
    
    min = values[0]
    for _, v := range values {
            if (v &lt; min) {
                min = v
            }
    }
    
    return min, nil
}

答案2

得分: 1

一般答案是:“是的,如果你不知道要比较的项目的确切数量,你必须使用循环。”

在这个中,Min函数的实现如下:

// 对于两个值
func Min(value_0, value_1 int) int {
	if value_0 < value_1 {
		return value_0
	}
	return value_1
}

// 对于1个或多个值
func Mins(value int, values ...int) int {
	for _, v := range values {
		if v < value {
			value = v
		}
	}
	return value
}
英文:

General answer is: "Yes, you must use a loop, if you do not know exact number of items to compare".

In this package Min functions are implemented like:

// For 2 values
func Min(value_0, value_1 int) int {
	if value_0 &lt; value_1 {
		return value_0
	}
	return value_1
}

// For 1+ values
func Mins(value int, values ...int) int {
	for _, v := range values {
		if v &lt; value {
			value = v
		}
	}
	return value
}

答案3

得分: 1

从Go 1.21开始,你可以使用内置函数min来找到最小值。

https://tip.golang.org/ref/spec#Min_and_max

英文:

Starting from go 1.21 you have an inbuilt function min for finding the minimum value

https://tip.golang.org/ref/spec#Min_and_max

答案4

得分: -3

你应该编写一个循环。在标准库中创建几十个函数来查找最小值/最大值/计数/按条件计数/全部满足条件/任意满足条件/没有满足条件等等,这样做没有意义,就像在C++中一样(大多数函数有4种不同的参数形式)。

英文:

You should write a loop. It does not make sense to create dozens of function in standard library to find min/max/count/count_if/all_of/any_of/none_of etc. like in C++ (most of them in 4 flavours according arguments).

huangapple
  • 本文由 发表于 2017年7月6日 13:56:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/44940822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定