The cmp package
The cmp package, which became part of the standard Go library in Go 1.21, contains types and functions for comparing ordered values. The reason for presenting it before the slices and maps packages is that it is used by the other two. Keep in mind that in its current version, the cmp package is simplistic but it might get enriched in the future with more functionality.
Under the hood, the cmp, slices, and maps packages use generics and constraints, which is the main reason for presenting them in this chapter. So, generics can be used for creating packages that work with multiple data types.
The important code of cmpPackage.go can be found in the main() function.
func main() {
fmt.Println(cmp.Compare(5, 4))
fmt.Println(cmp.Compare(4, 5))
fmt.Println(cmp.Less(4, 5.1))
}
Here, cmp.Compare(x, y) compares two values and returns -1 when x < y, 0 when x=y, and 1, when x > y. cmp.Compare(x, y) returns an int value. On the other...