Go strings.EqualFold
Compare two ASCII booking codes while ignoring uppercase and lowercase differences.
A simple example
strings.EqualFold
- Start with "a" and "A"
- Do this Compare ignoring case
- Result Equal
package main
import (
"fmt"
"strings"
)
func main() {
value := "a"
other := "A"
if strings.EqualFold(value, other) {
fmt.Println("yes")
} else {
fmt.Println("no")
}
}
yes
Syntax
strings.EqualFold("AB", "ab")
Run these examples in Go
Save one example at a time as main.go. Use Go 1.21 or later.
go run main.goMatch a booking code regardless of letter case
People enter short codes with different keyboard settings and capitalization. Match the input under the intended case rule so a valid code can be recognized without changing the stored identifier.
package main
import (
"fmt"
"strings"
)
func main() {
if strings.EqualFold("AB", "ab") {
fmt.Println("yes")
} else {
fmt.Println("no")
}
}
yes
A code printed as AB and a code entered as ab identify the same booking under this policy.
Accept an airport code typed in lowercase
A traveler types an airport code on a phone that defaults to lowercase. The lookup should recognize the airport even when the typed letters differ in case from the stored abbreviation.
package main
import (
"fmt"
"strings"
)
func main() {
value := "maa"
other := "MAA"
if strings.EqualFold(value, other) {
fmt.Println("yes")
} else {
fmt.Println("no")
}
}
yes
Only the capitalization differs.
Keep different seat numbers distinct
A reader compares two identifiers that may look similar on their labels. Use the full values under the intended case rules so a difference in one letter or digit is not lost.
package main
import (
"fmt"
"strings"
)
func main() {
value := "b12"
other := "B21"
if strings.EqualFold(value, other) {
fmt.Println("yes")
} else {
fmt.Println("no")
}
}
no
Ignoring case does not change digit order.
What to watch for
Use this policy only for fields whose specification permits it. Passwords and many identifiers are case-sensitive.