Find the minimum value in golang?

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

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

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

  1. values := []int{4, 20, 0, -11, -10}
  2. min := values[0]
  3. for _, v := range values {
  4. if (v < min) {
  5. min = v
  6. }
  7. }
  8. fmt.Println(min)

编辑

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

  1. func Min(values []int) (min int, e error) {
  2. if len(values) == 0 {
  3. return 0, errors.New("无法在空切片中找到最小值")
  4. }
  5. min = values[0]
  6. for _, v := range values {
  7. if (v < min) {
  8. min = v
  9. }
  10. }
  11. return min, nil
  12. }
英文:

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

  1. values := []int{4, 20, 0, -11, -10}
  2. min := values[0]
  3. for _, v := range values {
  4. if (v &lt; min) {
  5. min = v
  6. }
  7. }
  8. 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.

  1. func Min(values []int) (min int, e error) {
  2. if len(values) == 0 {
  3. return 0, errors.New(&quot;Cannot detect a minimum value in an empty slice&quot;)
  4. }
  5. min = values[0]
  6. for _, v := range values {
  7. if (v &lt; min) {
  8. min = v
  9. }
  10. }
  11. return min, nil
  12. }

答案2

得分: 1

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

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

  1. // 对于两个值
  2. func Min(value_0, value_1 int) int {
  3. if value_0 < value_1 {
  4. return value_0
  5. }
  6. return value_1
  7. }
  8. // 对于1个或多个值
  9. func Mins(value int, values ...int) int {
  10. for _, v := range values {
  11. if v < value {
  12. value = v
  13. }
  14. }
  15. return value
  16. }
英文:

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:

  1. // For 2 values
  2. func Min(value_0, value_1 int) int {
  3. if value_0 &lt; value_1 {
  4. return value_0
  5. }
  6. return value_1
  7. }
  8. // For 1+ values
  9. func Mins(value int, values ...int) int {
  10. for _, v := range values {
  11. if v &lt; value {
  12. value = v
  13. }
  14. }
  15. return value
  16. }

答案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:

确定