Menu Close

Go – bytes.Compare() function in go

bytes.Compare() function is used to compare two byte slices lexicographically in go and returns integer value.

bytes.Compare() function returns 0 if two byte slices are equal (a == b), -1 if first byte slice is less than second byte slice (a < b) and 1 if first byte slice is greater than second byte slice (a > b).

If the byte slices are empty then It will return nil.

bytes.Compare() function prototype:

func Compare(a, b []byte) int

or

func Compare(a []byte, b []byte) int

Input parameters:
a: First byte slice
b: Second byte slice

Here, type byte = uint8

bytes.Compare() function return value:

bytes.Compare() function compares each byte in two slices. If It find out that if first byte is greater than second byte then It returns 1 and if first byte is less than second byte then returns -1 and 0 If both the bytes are equal.

Explanation:

1) 
a := []byte("abc")
b := []byte("abc")

bytes.Compare(a, b) will return 0. Since both the byte slices are equal.

2)
first_byte_slice := []byte("xyz")
second_byte_slice := []byte("abc")

bytes.Compare(a, b) will return 1. Since 'x' in first_byte_slice is greater than 'a' in second_byte_slice.

3)
first_byte_slice := []byte("abc")
second_byte_slice := []byte("xyz")

bytes.Compare(a, b) will return -1. Since 'a' in first_byte_slice is less than 'x' in second_byte_slice.

4)
first_byte_slice := []byte("abcd")
second_byte_slice := []byte("abzd")

bytes.Compare(a, b) will return -1. Since 'c' in first_byte_slice is less than 'z' in second_byte_slice.

Example:

Code 1:

package main

import (
    "fmt"
    "bytes"
)

func main() {
    
    // Both the slices are equal
    fmt.Print("Both the slices are equal: ")
    byte_1 := []byte("abc")
    byte_2 := []byte("abc")
    
    fmt.Println(bytes.Compare(byte_1, byte_2))
    
    // First byte slice is greater than second byte slice
    fmt.Print("1st byte slices is greater than 2nd byte slice: ")
    byte_1 = []byte("xyz")
    byte_2 = []byte("abc")
    
    fmt.Println(bytes.Compare(byte_1, byte_2))
    
    // First byte slice is less than second byte slice
    fmt.Print("1st byte slices is less than 2nd byte slice: ")
    byte_1 = []byte("abc")
    byte_2 = []byte("xyz")
    
    fmt.Println(bytes.Compare(byte_1, byte_2))
    
}

Output:

Both the slices are equal: 0
1st byte slices is greater than 2nd byte slice: 1
1st byte slices is less than 2nd byte slice: -1

Code 2:

package main

import (
    "fmt"
    "bytes"
)

func main() {
    
    // Both the slices are equal
    fmt.Print("Both the slices are equal: ")
    byte_1 := []byte{10, 20, 30}
    byte_2 := []byte{10, 20, 30}
    
    fmt.Println(bytes.Compare(byte_1, byte_2))
    
    // First byte slice is greater than second byte slice
    fmt.Print("1st byte slices is greater than 2nd byte slice: ")
    byte_1 = []byte{10, 20, 30}
    byte_2 = []byte{1, 20, 30}
    
    fmt.Println(bytes.Compare(byte_1, byte_2))
    
    // First byte slice is less than second byte slice
    fmt.Print("1st byte slices is less than 2nd byte slice: ")
    byte_1 = []byte{10, 20, 30}
    byte_2 = []byte{10, 30, 30}
    
    fmt.Println(bytes.Compare(byte_1, byte_2))
    
}

Output:

Both the slices are equal: 0
1st byte slices is greater than 2nd byte slice: 1
1st byte slices is less than 2nd byte slice: -1

To learn more about golang, Please refer given below link:

References:

https://golang.org/doc/

Posted in bytes, golang, packages

Leave a Reply

Your email address will not be published.

Contact Us