How To Compare Two Slices In Golang With Example

Golang slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of items in the same slice.

Compare Two Slices In Golang

Golang bytes Compare() is an inbuilt function that returns an integer comparing two-byte slices lexicographically. The final result will be 0 if a==b, -1 if the a < b, and +1 if a > b. A nil argument is equivalent to the empty slice.

Syntax

func Compare(a, b []byte) int

Example

// hello.go

package main

import (
	"bytes"
	"fmt"
)

// Main function
func main() {

	// Creating and initializing
	// slices of bytes
	// Using shorthand declaration

	sliceA := []byte{'M', 'I', 'L', 'L', 'I', 'E'}
	sliceB := []byte{'B', 'O', 'B', 'B', 'Y'}

	// Comparing slice
	// Using Compare function
	res := bytes.Compare(sliceA, sliceB)

	if res == 0 {
		fmt.Println("Slices are equal")
	} else {
		fmt.Println("Slices are not equal")
	}
}

Output

go run hello.go
Slices are not equal

In the above example, sliceA and sliceB are not equal. So it will return Slices are not equal.

Let’s check a scenario where the slice bytes are equal.

// hello.go

package main

import (
	"bytes"
	"fmt"
)

// Main function
func main() {

	// Creating and initializing
	// slices of bytes
	// Using shorthand declaration

	sliceA := []byte{'B', 'O', 'B', 'B', 'Y'}
	sliceB := []byte{'B', 'O', 'B', 'B', 'Y'}

	// Comparing slice
	// Using Compare function
	res := bytes.Compare(sliceA, sliceB)

	if res == 0 {
		fmt.Println("Slices are equal")
	} else {
		fmt.Println("Slices are not equal")
	}
}

Output

go run hello.go
Slices are equal

Case sensitive check in byte[] Compare() function

If the values of both the byte[] are the same, but they differ in case sensitiveness, then it will return false. And byte[] Compare() function will returns that Slices are not equal.

// hello.go

package main

import (
	"bytes"
	"fmt"
)

// Main function
func main() {

	// Creating and initializing
	// slices of bytes
	// Using shorthand declaration

	sliceA := []byte{'B', 'o', 'b', 'b', 'Y'}
	sliceB := []byte{'B', 'O', 'B', 'B', 'Y'}

	// Comparing slice
	// Using Compare function
	res := bytes.Compare(sliceA, sliceB)

	if res == 0 {
		fmt.Println("Slices are equal")
	} else {
		fmt.Println("Slices are not equal")
	}
}

Output

go run hello.go
Slices are not equal

If both byte[] values are the same, but they have a different case, then it will not the same by Compare() function.

Compare two slices using Golang bytes.Equal(x, y)

Golang bytes.Equal() is an inbuilt function that reports whether x and y are the same length and contain the same bytes. A nil argument is equivalent to the empty slice.

Syntax

func Equal(x, y []byte) bool

Example

See the following code.

// hello.go

package main

import (
	"bytes"
	"fmt"
)

// Main function
func main() {

	// Creating and initializing
	// slices of bytes
	// Using shorthand declaration

	sliceA := []byte("Millie")
	sliceB := []byte("milLiE")

	// Comparing slice
	// Using Equal function
	res := bytes.Equal(sliceA, sliceB)

	if res {
		fmt.Println("Slices are equal")
	} else {
		fmt.Println("Slices are not equal")
	}
}

Output

go run hello.go
Slices are not equal

In the above code, both bytes have the same values and length but differ in character cases; that is why they are not the same.

Let’s take a scenario in which everything is the same.

// hello.go

package main

import (
	"bytes"
	"fmt"
)

// Main function
func main() {

	// Creating and initializing
	// slices of bytes
	// Using shorthand declaration

	sliceA := []byte("Millie")
	sliceB := []byte("Millie")

	// Comparing slice
	// Using Equal function
	res := bytes.Equal(sliceA, sliceB)

	if res {
		fmt.Println("Slices are equal")
	} else {
		fmt.Println("Slices are not equal")
	}
}

Output

go run hello.go
Slices are equal

Recursive comparison for byte slices

Golang DeepEqual function from reflect package used to check x and y are “deeply equal”. This applies to Array values that are deeply equal when their corresponding items are deeply equal.

Struct values are deeply equal if their corresponding fields, both exported and unexported, are deeply equal.

Let’s take integer slice in the following example and check if both slices are equal or not.

// hello.go

package main

import (
	"fmt"
	"reflect"
)

// Main function
func main() {

	// Creating and initializing
	// slices of bytes
	// Using shorthand declaration

	sliceA := []int{1, 2, 3, 11}
	sliceB := []int{1, 2, 3, 4}

	if reflect.DeepEqual(sliceA, sliceB) {
		fmt.Println("Slices are equal")
	} else {
		fmt.Println("Slices are not equal")
	}
}

Output

go run hello.go
Slices are not equal

In the above code, we have taken two integer slices: sliceA and sliceB.

Both slices have different values that’s why the Golang DeepEqual() function returns false.

Let”s take the same values for both slices and see the output.

// hello.go

package main

import (
	"fmt"
	"reflect"
)

// Main function
func main() {

	// Creating and initializing
	// slices of bytes
	// Using shorthand declaration

	sliceA := []int{11, 21, 18, 19}
	sliceB := []int{11, 21, 18, 19}

	if reflect.DeepEqual(sliceA, sliceB) {
		fmt.Println("Slices are equal")
	} else {
		fmt.Println("Slices are not equal")
	}
}

Output

go run hello.go
Slices are equal

Conclusion

Golang slice allows us to compare two slices of the byte type with each other using bytes.Compare() function. The Compare() function returns an integer value which represents that these slices are equal or not and the values are:

  1. If the result is 0, then sliceA == sliceB.
  2. If the result is -1, then sliceA < sliceB.
  3. If the result is +1, then sliceA > sliceB.

Golang Equal(a, b) function reports whether sliceA and sliceB are the same length and contain the same bytes. A nil argument is equivalent to the empty slice.

Golang reflect.DeepEqual(a, b interface{}) function reports whether a and b are “deeply equal,” defined as follows.

Slice values are deeply equal when all of the following are true: they are both nil or both non-nil, they have the same length, and either they point to the same initial entry of the same underlying array (that is, &x[0] == &y[0]) or their corresponding elements (up to length) are deeply equal. Note that a non-nil empty slice and a nil slice (for example, []byte{} and []byte(nil)) are not deeply equal.

Finally, Compare Two Slices In Golang With Example Tutorial is over.

See also

Golang Serialize JSON String

Compare two strings in Golang

Golang String to Byte array

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.