notexOPENOverview
Notex/Languages/Comparing Slices
01.14LESSON 14 OF 33 · 2 MIN READ

Comparing Slices

Always free to read

As I mentioned earlier, slices are not comparable, which means they cannot be compared to each other using the == and != operators (slices can only be compared to nil). If you need to test if two slices have identical sizes and contents, the slices package in the standard library provides two functions. The slices.Equal function takes in two slices and returns true if the slices are the same length, and all of the elements are equal. It requires the elements of the slice to be comparable. The other function, slices.EqualFunc, lets you pass in a function to determine equality and does not require the slice elements to be comparable. You’ll learn about passing functions into functions in “Passing Functions as Parameters”.

You can see how Equal and EqualFunc are used on the Go Playground:

x := []int{1, 2, 3}
y := []int{4, 5, 6}
z := []int{1, 2, 3}
fmt.Println(slices.Equal(x, y)) // prints false
fmt.Println(slices.Equal(x, z)) // prints true
a := [][]int{{1, 2, 3}, {4, 5, 6}}
b := [][]int{{1, 2, 3}, {4, 5, 6}}
// pass slices.Equal in to slices.EqualFunc to
// compare elements that are slices of comparable values
fmt.Println(slices.EqualFunc(a, b, slices.Equal)) // prints true

Slices are also not an ordered type. That means you cannot use them with the <, <=, >, and >= operators. Just like equality testing, the slices package provides two functions to determine if one slice is greater than, equal to, or less than another slice. The first is Compare and is only usable if the elements of the slice are an ordered type (only integers types, floating point types, and string are ordered types). Compare returns -1 if the first slice is “less than” the second slice, 1 if the first slice is “greater than” the second slice, and 0 if the slices are identical. If the elements are not an ordered type, use the CompareFunc function, passing in the slices and a function that compares an element of the slice. You can see how Compare and CompareFunc are used on the Go Playground:

x := []int{1, 2, 3}
y := []int{4, 5, 6}
z := []int{1, 2, 3}
fmt.Println(slices.Compare(x, y)) // prints -1
fmt.Println(slices.Compare(x, z)) // prints 0
a := [][]int{{1, 2, 3}, {4, 5, 6}}
b := [][]int{{1, 2, 3}, {4, 5, 6}}
// pass slices.Compare in to slices.CompareFunc to
// compare elements that are slices of ordered values
fmt.Println(slices.CompareFunc(a, b, slices.Compare)) // prints 0

Warning

The reflect package contains a function called DeepEqual that can compare almost anything, including slices. It’s a legacy function, primarily intended for testing. Before the inclusion of slices.Equal and slices.EqualFunc, reflect.DeepEqual was often used to compare slices. Don’t use it in new code, as it is slower and less safe than using the functions in the slices package.

Progress is saved on this device.
KEEP EXPLORING

One note leads to another.

Back to the library →
⌕
Search across the entire library ⌘ K