English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang Basic Tutorial

Golang Control Statements

Golang Function & Method

Golang Struct

Golang Slice & Array

Golang String (String)

Golang Pointer

Golang Interface

Golang Concurrency

Golang Error

Golang Other Miscellaneous

Go Language Slice Comparison

In Go language, slices are more powerful, flexible, convenient, and lightweight data structures than arrays. Slices are variable-length sequences used to store elements of the same type and do not allow different types of elements to be stored in the same slice. In Go slices, you can useCompare()The function compares two slices of byte type with each otherComparison。This function returns an integer value indicating whether the slices are equal or not, and these values are:

  • If the result is 0, then slice_1 == slice_2。

  • If the result is-1,则slice_1 <slice_2。

  • If the result is+1,则slice_1> slice_2。

This function is defined under the bytes package, so you must import the bytes package into your program to access the Compare function.

Syntax:

func Compare(slice_1, slice_2 []byte) int

Let's discuss this concept with an example:

//Comparing two byte slices
package main
import (
    "bytes"
    "fmt"
)
func main() {
    //Using shorthand declaration to create and initialize byte slices
    slice_1 := []byte{'G', 'E', 'E', 'K', 'S'}
    slice_2 := []byte{'G', 'E', 'e', 'K', 'S'}
    //Comparing slices
    //Using Compare function
    res := bytes.Compare(slice_1, slice_2)
    if res == 0 {
        fmt.Println("!..Slices are equal..!")
    } else {
        fmt.Println("!..Slices are not equal..!")
    }
}

Output:

!..Slices are not equal..!

Comparing two byte slices example:

package main
import (
    "bytes"
    "fmt"
)
func main() {
    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 slice
    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
    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)
    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