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:
// 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
Recommended Posts:
- How to check String for equality under Unicode case folding in Golang?
- How to convert a slice of bytes in title case in Golang?
- How to check equality of slices of bytes in Golang?
- Golang | Checking the byte of slice for specified regular expression
- How to split a slice of bytes in Golang?
- How to repeat a slice of bytes in Golang?
- How to trim a slice of bytes in Golang?
- How to convert a slice of bytes in uppercase in Golang?
- How to convert a slice of bytes in lowercase in Golang?
- How to replace all the elements in slice of bytes in Golang?
- How to trim prefix from the slice of bytes in Golang?
- How to find the first index value in slice of bytes in Golang?
- How to split a slice of bytes after the given separator in Golang?
- Golang | Splitting a slice of bytes after the specified separator
- How to trim suffix from the slice of bytes in Golang?
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.



