Standard library / slices

slices.Compare()

Lexicographically compares ordered slices; if the common prefix is equal, the shorter slice sorts first. NaN sorts before non-NaN. Expected stdout: '-1\n'.

Formal Syntax & Structure

go syntaxgo
func Compare[S ~[]E, E cmp.Ordered](s1, s2 S) int

Parameters & Attributes

NameTypeDescription
s1SFirst slice to compare.
s2SSecond slice to compare.

Return Value

int

Lexicographically compares ordered slices; if the common prefix is equal, the shorter slice sorts first. NaN sorts before non-NaN.

Working Example

Example Snippetgo
package main
import (  "fmt"  "slices")
func main() {  fmt.Println(slices.Compare([]int{1, 2}, []int{1, 3}))}
Go Reference