EqualFold() function in the Golang strings package is used to check if two strings are equal. It returns a true value if two strings are equal else false. Here strings are interpreted as UTF-8 strings and comparison is performed under Unicode case-folding.
The string comparison using the EqualFold() function is not case-sensitive.
EqualFold() Syntax
The syntax for EqualFold() function is:
func EqualFold(s1, s2 string) bool
EqualFold() Parameters:
The EqualFold() function take two parameters
s1 is the string
s2 is the string
EqualFold() Return Value
The strings package EqualFold() function returns a true value if both strings are equal else false.
Let’s understand the string package EqualFold() method with examples.
strings.EqualFold() function to check if two strings are equal in Go
The following go program checks if two strings are equal using the strings.EqualFold() function.
package main import ( "fmt" "strings" ) func main() { // Check if two strings are matches with each other. Comparision is not case-sensitive fmt.Println(strings.EqualFold("GolangSpot", "GoLangSpot")) fmt.Println(strings.EqualFold("AreStringsMatch", "arestringsmatch")) }
The output of the string comparison using the EqualFold() in go are:
true
true
In the above program,
The first output returns the true value as both strings are matched with each other and are equal. The comparison is not case-sensitive.
The second output returns the true value as both strings are equal. Both strings are interpreted as UTF-8 strings and comparison is done under Unicode case-folding.
Cool Tip: How to use the string Count function in Golang!
Conclusion
I hope the above article on the EqualFold() method to find if two strings are equal or not and return a boolean value is helpful to you.
You can find more topics about the Golang tutorials on the GolangSpot Home page.