Golang String EqualFold() is an inbuilt function that reports whether s and t, interpreted as UTF-8 strings, are equal under Unicode case-folding, which is a more general form of case-insensitivity. Uppercase letters are not equal to lowercase letters. But with EqualFold() function, we fold the cases.
This makes “A” equal to “a.” No string copies or conversions are required.
Golang String EqualFold()
In Golang, we can use the equality operator to test the contents of strings. This treats uppercase and lowercase characters as different. Golang strings.EqualFold() can be used for case insensitivity. That means if the rune is the same, but the cases are different, then still it returns true.
Syntax
func EqualFold(s, t string) bool
Parameters
The function takes two arguments, and both are strings. We check against each other, and if the runes are the same, then regardless of the cases, it will return true.
Example
// hello.go package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.EqualFold("QUIBI", "quibi")) }
Output
go run hello.go true
In the above code, both strings are the same, but the cases are different. One is uppercase, and one is lowercase.
Let’s see another example.
// hello.go package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.EqualFold("peacock", "quibi")) }
Output
go run hello.go false
Conclusion
With the equals-sign operator and strings.EqualFold(), we can compare strings. Case-insensitivity is not always needed. Using the equals operator is best when it is sufficient.