The EqualFold
function in Golang is used to check if two strings are equal. The comparison is not case-sensitive.
Note: Here, the strings are interpreted as UTF-8 strings, and all comparisons are done under Unicode case-folding, a more generalized version of case insensitive comparisons.
To use this function, we must import the strings
package in our file and access the EqualFold
function within it using the .
notation (strings.EqualFold
).
The following is the definition of the EqualFold
function:
func EqualFold(x, y string) bool
The EqualFold
function takes two arguments, which represent the two string
values we want to check the equality of.
The EqualFold
function returns true
if both string
arguments are equal (under Unicode case-folding). Otherwise, false
is returned.
In the code below, we use the EqualFold
function to check the equality of two strings and show that comparisons are not case sensitive:
package mainimport ("fmt""strings")func main() {up_str := "EDUCATIVE"low_str := "educative"// Example 1// Comparing upper case stringscheck1 := strings.EqualFold(up_str, up_str)if check1{fmt.Println(up_str, "and", up_str ,"both match")} else{fmt.Println(up_str, "and", up_str ,"dont match")}// Example 2// Comparing upper and lower case stringscheck2 :=strings.EqualFold(up_str, low_str)if check2{fmt.Println(up_str, "and", low_str ,"both match")} else{fmt.Println(up_str, "and" ,low_str ,"dont match")}}