Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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..!")
    }
  
}


Output:

!..Successfully Matched..!

Example 2:




// 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)
  
}


Output:

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


Last Updated : 26 Aug, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads