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) int

Parameters & Attributes

NameTypeDescription
s1S1First slice to compare.
s2S2Second slice to compare.
cmpfunc(E1, E2) intComparison 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) }))}
Go Reference