In Go, you can use the strings.ToLower
function to convert both strings to lowercase and then compare them using the ==
operator. Here's an example of how to implement a case-insensitive string comparison in Go:
package main
import (
"fmt"
"strings"
)
func main() {
str1 := "Hello"
str2 := "hello"
if strings.ToLower(str1) == strings.ToLower(str2) {
fmt.Printf("%s and %s are equal (case-insensitive)\n", str1, str2)
} else {
fmt.Printf("%s and %s are not equal (case-insensitive)\n", str1, str2)
}
}
Output:
Hello and hello are equal (case-insensitive)
By converting both strings to lowercase using strings.ToLower
, we ensure that the comparison is performed in a case-insensitive manner.