Standard library / slices
slices.CompareFunc()
Lexicographic comparison using a callback that can compare different element types. Expected stdout: '0\n'.
Formal Syntax & Structure
go syntaxgo
func CompareFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, cmp func(E1, E2) int) intParameters & Attributes
| Name | Type | Description |
|---|---|---|
| s1 | S1 | First slice to compare. |
| s2 | S2 | Second slice to compare. |
| cmp | func(E1, E2) int | Comparison callback; negative, zero, or positive for less, equivalent, or greater. |
Return Value
int
Lexicographic comparison using a callback that can compare different element types.
Working Example
Example Snippetgo
package main
import ( "cmp" "fmt" "slices")
func main() { fmt.Println(slices.CompareFunc([]string{"aa"}, []int{2}, func(s string, n int) int { return cmp.Compare(len(s), n) }))}