golang

Learn How to compare slices in Golang


In this article, you will learn about how to compare slices in the Go language.

In Go language, slice stores a similar type of element which is a sequence of variable length. It is similar to an array but the main difference is the size of a slice can be resized on the other hand an array size is fixed. You can not compare slices by using a simple equality comparison in the Go language. To compare slices, you need to write a custom function. Let’s see an example of it:

package main

import "fmt"

func compareStringSlices(a, b []string) bool {
        if len(a) != len(b) {
                return false
        }
        for i, v := range a {
                if v != b[i] {
                        return false
                }
        }
        return true
}

func main() {
        slice1  := []string{"Deven"}
        slice2  := []string{"Deven"}
        fmt.Printf("Slices Equal: %t\\n", compareStringSlices(slice1 , slice2))

        slice3  := []string{"Deven"}
        slice4  := []string{"Rathore"}
        fmt.Printf("Slices Equal: %t\\n", compareStringSlices(slice3, slice4))
}

/*
      Output:
        Slices Equal: true
        Slices Equal: false
*/

Here, you can see that first, it gives the result true as the two slices are the same and for the second one it gives false as the two slices are not the same.

You can also compare the byte type of the two slices. To do so all you need is to use the compare() function. See the below code example to get a clear understanding:

package main

import (
        "bytes"
        "fmt"
)

func main() {

        slice1 := []byte{'D', 'E', 'V', 'E', 'N'}
        slice2 := []byte{'D', 'E', 'V', 'E', 'N'}

        res := bytes.Compare(slice1, slice2)

        if res == 0 {
                fmt.Println("Slices Equal")
        } else {
                fmt.Println("Slices Not Equal")
        }
}

// Output: Slices Equal

In the Go language, the compare function is case-sensitive. That means, even if the two slices are the same but the letters are uppercase or lowercase then it will return as false. See the example below:

package main

import (
        "bytes"
        "fmt"
)

func main() {

        slice1 := []byte{'D', 'E', 'V', 'E', 'N'}
        slice2 := []byte{'d', 'E', 'V', 'E', 'N'}

        res := bytes.Compare(slice1, slice2)

        if res == 0 {
                fmt.Println("Slices Equal")
        } else {
                fmt.Println("Slices Not Equal")
        }
}

// Output: Slices Not Equal

This compare() function returns 0 when slices are equal and return -1 if slice1 is smaller than slice2 and returns +1 if slice1 is greater than slice2. Let’s see the below code example:

package main

import (
        "bytes"
        "fmt"
)

func main() {

        slice1 := []byte{'D', 'E', 'V', 'E', 'N'}
        slice2 := []byte{'R', 'A', 'T', 'H', 'O', 'R', 'E'}
        slice3 := []byte{'D', 'E', 'V', 'E', 'N'}
        slice4 := []byte{'A', 'L', 'E', 'X'}

        res1 := bytes.Compare(slice1, slice2)
        res2 := bytes.Compare(slice1, slice3)
        res3 := bytes.Compare(slice1, slice4)

        fmt.Println("\\nResult 1:", res1)
        fmt.Println("Result 2:", res2)
        fmt.Println("Result 3:", res3)

}

/*
      Output:
        Result 1: -1
        Result 2: 0
        Result 3: 1
*/

Here, you can see that the first result is -1 because slice1 is smaller than slice2 and the second result is 0 because slice1 and slice3 are equal and finally, the result is 1 because silice1 is greater than slice4.

This is how you can compare slices in the Go language by using compare() function or creating your own custom function.


Share on social media

//