What is the EqualFold function in Golang?

Overview

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).

Syntax

The following is the definition of the EqualFold function:

func EqualFold(x, y string) bool

Parameters

The EqualFold function takes two arguments, which represent the two string values we want to check the equality of.

Return value

The EqualFold function returns true if both string arguments are equal (under Unicode case-folding). Otherwise, false is returned.

Code

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 main
import (
"fmt"
"strings"
)
func main() {
up_str := "EDUCATIVE"
low_str := "educative"
// Example 1
// Comparing upper case strings
check1 := 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 strings
check2 :=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")
}
}
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources