Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang
communitycreator

What is the EqualFold function in Golang?

Faraz Karim

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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")
}
}
Example of EqualFold method

RELATED TAGS

golang
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring