Standard library / cmp

cmp.Ordered

Generic constraint for integer, floating-point and string types, including defined types with those underlying types. Not an ordinary value interface. Expected stdout: '2\n'.

Formal Syntax & Structure

go syntaxgo
type Ordered interface {  ~int | ~int8 | ~int16 | ~int32 | ~int64 |    ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |    ~float32 | ~float64 |    ~string}

Working Example

Example Snippetgo
package main
import (  "cmp"  "fmt")
func smaller[T cmp.Ordered](a, b T) T {  if cmp.Less(a, b) {    return a  }  return b}func main() {  fmt.Println(smaller(4, 2))}
Go Reference