What Is the comparable Constraint in Go

The comparable constraint restricts generic type parameters to types that support equality comparison operators.

The comparable constraint is a predeclared interface that restricts generic type parameters to types that support equality comparison using == or !=. It includes booleans, numbers, strings, pointers, channels, and structs composed of comparable fields, but excludes slices, maps, and functions. You use it in generic function or type definitions to ensure the type parameter can be compared.

func Contains[T comparable](slice []T, item T) bool {
	for _, v := range slice {
		if v == item {
			return true
		}
	}
	return false
}