gogenericscomparable

invalid operation: v > max (type parameter T is not comparable with >)


package main

import (
    "fmt"
)

func findMinMax[T comparable](arr []T) (min, max T) {
    for _, v := range arr {
        if v > max {
            max = v
        } else if v < min {
            min = v
        }
    }
    return min, max
}

func main() {
    arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
    fmt.Println(findMinMax(arr))
}

I would be more than happy to help you resolve the issue you are facing in the findMinMax function. The error message indicating that v > max or v < min suggests that there might be a problem with the comparison operation within the function. In order to offer a precise solution, I would need to see the implementation of the findMinMax function. Based on your description, it seems that the type T, which is supposed to be comparable, is causing the issue during comparison.

I am expecting the function findMinMax will work correctly.


Solution

  • You used the comarable constraint for the T type parameter. comparable means that: comparable. So you can use the == operator on values of that type. It doesn't mean they are ordered, which is required to use the < > operators.

    The ordered constraint is defined in the golang.org/x/exp/constraints package, see constraints.Ordered.

    Using that your code compiles:

    import (
        "fmt"
    
        "golang.org/x/exp/constraints"
    )
    
    func findMinMax[T constraints.Ordered](arr []T) (min, max T) {
        for _, v := range arr {
            if v > max {
                max = v
            } else if v < min {
                min = v
            }
        }
        return min, max
    }
    

    Try it on the Go Playground.

    It gives wrong result though, as you're starting min and max from their zero values, and if all values in the passed slice are greater than or less than the zero value, min or max will remain the zero value.

    An easy fix is to initialize min and max with the first value if the passed slice is not empty:

    func findMinMax[T constraints.Ordered](arr []T) (min, max T) {
        if len(arr) > 0 {
            min, max = arr[0], arr[0]
        }
    
        for _, v := range arr {
            if v > max {
                max = v
            } else if v < min {
                min = v
            }
        }
        return min, max
    }
    

    This will output (try it on the Go Playground):

    1 9
    

    Note if you're working with floating point types, you have to explicitly handle NaN values as their order to other floats is not specified.