Open In App

How to check equality of slices of bytes in Golang?

Last Updated : 02 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads