Open In App

How to compare two slices of bytes in Golang?

Last Updated : 01 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 type of elements in the same slice. In the Go slice, you are allowed to compare two slices of the byte type with each other using Compare() function. This function returns an integer value that represents whether these slices are equal or not and the values are:

  • If the result is 0, then slice_1 == slice_2.
  • If the result is -1, then slice_1 < slice_2.
  • If the result is +1, then slice_1 > slice_2.

This function is defined under the bytes package so, you have to import the bytes package in your program for accessing Compare function. 

Syntax:

func Compare(slice_1, slice_2 []byte) int

In Go, you can compare two 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 compare 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 compare two slices of bytes in Go, and it’s widely used in Go programs for this purpose.

Let us discuss this concept with the help of the examples: 

Example 1: 

Go




// Go program to illustrate how to
// compare two slices of bytes
package main
 
    import("bytes"
           "fmt")
 
    // Main function
    func main()
{
 
    // Creating and initializing
    // slices of bytes
    // Using shorthand declaration
 
slice_1:
    = [] byte { 'G', 'E', 'E', 'K', 'S' } slice_2
        : = [] byte { 'G', 'E', 'e', 'K', 'S' }
 
          // Comparing slice
          // Using Compare function
          res : = bytes.Compare(slice_1, slice_2)
 
                      if res
                  == 0
    {
        fmt.Println("!..Slices are equal..!")
    }
    else { fmt.Println("!..Slice are not equal..!") }
}


Output:

!..Slice are not equal..!

Example 2: 

Go




// Go program to illustrate how
// to compare two slices of byte
package main
 
import (
    "bytes"
    "fmt"
)
 
func main() {
 
    // Creating and initializing
    // slices of bytes
    // Using shorthand declaration
    slice_1 := []byte{'A', 'N', 'M', 'O', 'P', 'Q'}
    slice_2 := []byte{'a', 'g', 't', 'e', 'q', 'm'}
    slice_3 := []byte{'A', 'N', 'M', 'O', 'P', 'Q'}
    slice_4 := []byte{'A', 'n', 'M', 'o', 'p', 'Q'}
 
    // Displaying slices
    fmt.Println("Slice 1: ", slice_1)
    fmt.Println("Slice 2: ", slice_2)
    fmt.Println("Slice 3: ", slice_3)
    fmt.Println("Slice 4: ", slice_4)
 
    // Comparing slices
    // Using Compare function
    res1 := bytes.Compare(slice_1, slice_2)
    res2 := bytes.Compare(slice_1, slice_3)
    res3 := bytes.Compare(slice_1, slice_4)
    res4 := bytes.Compare(slice_2, slice_3)
    res5 := bytes.Compare(slice_2, slice_4)
    res6 := bytes.Compare(slice_2, slice_1)
    res7 := bytes.Compare(slice_3, slice_1)
    res8 := bytes.Compare(slice_3, slice_2)
    res9 := bytes.Compare(slice_3, slice_4)
    res10 := bytes.Compare(slice_4, slice_1)
    res11 := bytes.Compare(slice_4, slice_2)
    res12 := bytes.Compare(slice_4, slice_3)
    res13 := bytes.Compare(slice_4, slice_4)
 
    // Displaying results
    fmt.Println("\nResult 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)
    fmt.Println("Result 7:", res7)
    fmt.Println("Result 8:", res8)
    fmt.Println("Result 9:", res9)
    fmt.Println("Result 10:", res10)
    fmt.Println("Result 11:", res11)
    fmt.Println("Result 12:", res12)
    fmt.Println("Result 13:", res13)
}


Output:

Slice 1:  [65 78 77 79 80 81]
Slice 2:  [97 103 116 101 113 109]
Slice 3:  [65 78 77 79 80 81]
Slice 4:  [65 110 77 111 112 81]

Result 1: -1
Result 2: 0
Result 3: -1
Result 4: 1
Result 5: 1
Result 6: 1
Result 7: 0
Result 8: -1
Result 9: -1
Result 10: 1
Result 11: -1
Result 12: 1
Result 13: 0


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

Similar Reads