How to check equality of slices of bytes in Golang?
Last Updated : 02 Mar, 2023
In Go language slice is more powerful, flexible, and convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence that stores elements of a similar type, you are not allowed to store different types of elements in the same slice. In the Go slice of byes, you are allowed to check the equality of the slices with the help of Equal() function. This function returns true if both the slices are equal, or returns false if both the slices are not equal. It is defined under the bytes package so, you have to import bytes package in your program for accessing Equals function.
Syntax:
func Equal(slice_1, slice_1 []byte) bool
In Go, you can check the equality of slices of bytes using the built-in bytes.Equal function from the bytes package. The bytes.Equal function takes two arguments, both of type []byte, and returns a boolean indicating whether the two slices are equal or not.
Here's an example that demonstrates how to check the equality of two slices of bytes in Go:
Go package main import ( "bytes" "fmt" ) func main() { slice1 := []byte{1, 2, 3, 4, 5} slice2 := []byte{1, 2, 3, 4, 5} slice3 := []byte{5, 4, 3, 2, 1} fmt.Println(bytes.Equal(slice1, slice2)) // true fmt.Println(bytes.Equal(slice1, slice3)) // false }
Output:
true false
In this example, the two slices slice1 and slice2 are equal, and the bytes.Equal function returns true. The two slices slice1 and slice3 are not equal, and the bytes.Equal function returns false.
The bytes.Equal function is a simple and efficient way to check the equality of two slices of bytes in Go, and it's widely used in Go programs for this purpose.
Here, we check the equality between slice_1 and slice_2 and the return type of this function is bool. Let us discuss this concept with the help of the examples:
Example 1:
Go // Go program to illustrate how to // check the equality of the slices package main import ( "bytes" "fmt" ) func main() { // Creating and initializing slices of bytes // Using shorthand declaration slice_1 := []byte{'A', 'N', 'M', 'A', 'P', 'A', 'A', 'W'} slice_2 := []byte{'A', 'N', 'M', 'A', 'P', 'A', 'A', 'W'} // Checking the equality of the slices // Using Equal function res := bytes.Equal(slice_1, slice_2) if res == true { fmt.Println("Slice_1 is equal to Slice_2") } else { fmt.Println("Slice_1 is not equal to Slice_2") } }
Output:
Slice_1 is equal to Slice_2
Example 2:
Go // Go program to illustrate how to // check the equality of the slices package main import ( "bytes" "fmt" ) func main() { // Creating and initializing // slices of bytes // Using shorthand declaration slice_1 := []byte{'A', 'N', 'M', 'A', 'P', 'A', 'A', 'W'} slice_2 := []byte{'g', 'e', 'e', 'k', 's'} slice_3 := []byte{'A', 'N', 'M', 'A', 'P', 'A', 'A', 'W'} // Checking the equality of the slices // Using Equal function res1 := bytes.Equal(slice_1, slice_2) res2 := bytes.Equal(slice_1, slice_3) res3 := bytes.Equal(slice_2, slice_3) res4 := bytes.Equal([]byte("GeeksforGeeks"), []byte("GeeksforGeeks")) res5 := bytes.Equal([]byte("Geeks"), []byte("GFG")) res6 := bytes.Equal(slice_1, []byte("P")) // Displaying results fmt.Println("Result 1:", res1) fmt.Println("Result 2:", res2) fmt.Println("Result 3:", res3) fmt.Println("Result 4:", res4) fmt.Println("Result 5:", res5) fmt.Println("Result 6:", res6) }
Output:
Result 1: false Result 2: true Result 3: false Result 4: true Result 5: false Result 6: false
Similar Reads
How to Compare Equality of Struct, Slice and Map in Golang? In Golang, reflect.DeepEqual function is used to compare the equality of struct, slice, and map in Golang. It is used to check if two elements are "deeply equal" or not. Deep means that we are comparing the contents of the objects recursively. Two distinct types of values are never deeply equal. Two
3 min read
How to compare two slices of bytes in Golang? In Go, a slice is a powerful, flexible data structure that allows elements of the same type to be stored in a variable-length sequence. When working with byte slices ([]byte), Go provides easy-to-use functions in the bytes package to compare them. In this article we will learn "How to compare two sl
2 min read
How to split a slice of bytes in Golang? In Golang, you can split a slice of bytes into multiple parts using the bytes.Split function. This is useful when dealing with data like encoded strings, file contents, or byte streams that must be divided by a specific delimiter.Examplepackage mainimport ( "bytes" "fmt")func main() { // Initial byt
3 min read
How to trim a slice of bytes in Golang? In Go language 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 elements in the same slice. In the Go slice of bytes, you ar
3 min read
How to sort a slice of ints in Golang? In Go, slices provide a flexible way to manage sequences of elements. To sort a slice of ints, the sort package offers a few straightforward functions. In this article we will learn How to Sort a Slice of Ints in Golang.ExampleGopackage main import ( "fmt" "sort" ) func main() { intSlice := []int{42
2 min read