Case insensitive string comparison in Go
https://golang.org/pkg/strings/#EqualFold is the function you are looking for. It is used like this (example from the linked documentation):
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.EqualFold("Go", "go"))
}
method 1:
func CompareStringsInCrudeWay(stringA, stringB string) (bool, error) { if strings.ToLower(stringA) == strings.ToLower(stringB) { return true, nil } else { return false, nil } }
method 2:
func Compare(stringA, stringB string) bool { for i := 0; i < len(stringA); i++ { if stringA[i] == stringB[i] { continue } if unicode.ToLower(stringA[i]) != unicode.ToLower(stringB[i]) { return false } } return true }
method 3:
func CompareStringsInEfficientWay(stringA, stringB string) (bool, error) { if strings.EqualFold(stringA, stringB) { return true, nil } else { return false, nil } }
method 3 is actually wrapping method 2, and both are efficient. You can check this blog for more explanation.