Checking Slice of bytes for equality under Unicode case folding in Golang

In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice.
In the Go slice of bytes, you are allowed to compare two slices with each other without any error even if the elements of these slices are written in different cases(lowercase and uppercase) with the help of EqualFold() function. Or in other, this function is used to check whether the specified slices are interpreted as UTF-8 strings and are equal under Unicode case-folding. This function returns true if the given slices are equal under Unicode case-folding or return false if the given slices are not equal under Unicode case-folding. It is defined under the bytes package so, you have to import bytes package in your program for accessing EqualFold function.

Syntax:

func EqualFold(slice_1, slice1_2 []byte) bool

The return type of this function is of the bool type. Let us discuss this concept with the help of the examples:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to check
// the given slices are equal or not
package main
  
import (
    "bytes"
    "fmt"
)
  
// Main function
func main() {
  
    // Creating and initializing slices of bytes
    // Using shorthand declaration
    username := []byte{'G', 'E', 'E', 'K', 'S'}
    uinput := []byte{'g', 'e', 'e', 'k', 's'}
  
    // Checking whether the given slices are equal or not
    // Using EqualFold() function
    res := bytes.EqualFold(username, uinput)
  
    // Displaying the result, according
    // to the given condition
    if res == true {
      
        fmt.Println("!..Successfully Matched..!")
    } else {
      
        fmt.Println("!..Not Matched..!")
    }
  
}

chevron_right


Output:

!..Successfully Matched..!

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to check
// the given slices are equal or not
package main
  
import (
    "bytes"
    "fmt"
)
  
func main() {
  
    // Creating and initializing slices of bytes
    // Using shorthand declaration
    slice_1 := []byte{'G', 'E', 'E', 'K', 'S', 'F',
             'o', 'r', 'G', 'E', 'E', 'K', 'S'}
  
    slice_2 := []byte{'g', 'e', 'e', 'k', 's', 'f',
             'o', 'r', 'g', 'e', 'e', 'k', 's'}
  
    slice_3 := []byte{'A', 'P', 'P', 'L', 'E'}
  
    // Checking whether the given
    // slices are equal or not
    // Using EqualFold() function
    res1 := bytes.EqualFold(slice_1, slice_2)
    res2 := bytes.EqualFold(slice_2, slice_3)
    res3 := bytes.EqualFold(slice_3, []byte("apple"))
    res4 := bytes.EqualFold([]byte("Geeks"), []byte("GEEks"))
  
    // Displaying results
    fmt.Println("Result_1: ", res1)
    fmt.Println("Result_2: ", res2)
    fmt.Println("Result_3: ", res3)
    fmt.Println("Result_4: ", res4)
  
}

chevron_right


Output:

Result_1:  true
Result_2:  false
Result_3:  true
Result_4:  true


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.