Go Reference · strings and strconv
Go strings.ToUpper() and ToLower() functions
Reference for Go strings.ToUpper, ToLower, ToTitle and EqualFold: convert case with full Unicode support, compare case-insensitively, and avoid common title-casing mistakes.
strings.ToUpper(s) and strings.ToLower(s) return a copy of s with every letter mapped to upper or lower case according to Unicode rules, so they work for accented letters and non-Latin alphabets, not just ASCII.
For comparing two strings without caring about case, use strings.EqualFold, which applies Unicode case folding and allocates nothing.
strings.ToUpper("go") // "GO"
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
func ToUpper(s string) string func ToLower(s string) string func ToTitle(s string) string func ToUpperSpecial(c unicode.SpecialCase, s string) string func ToLowerSpecial(c unicode.SpecialCase, s string) string func EqualFold(s, t string) bool
| Name | Description |
|---|---|
s, t |
The strings to convert or compare. |
c |
A language-specific rule set, such as unicode.TurkishCase, for the ...Special variants. |
Returns: the converted string, or for EqualFold a bool reporting whether s and t are equal under simple Unicode case folding.
Edge cases:
- Mapping is rune by rune, so
ToUpper("ß")stays"ß"(Go uses simple case mapping, not full), andEqualFold("Straße", "STRASSE")isfalse. - A string already in the target case is returned as is, without allocation.
- Final sigma and language-specific rules (Turkish dotted/dotless i) are not handled by
ToLower; use theSpecialvariants for those.
Usage notes
- Don't use
==on lowercased strings for security checks like usernames without thinking about normalization; two different Unicode spellings can look identical. EqualFoldbeatsToLower(a) == ToLower(b): no allocation, and it handles characters like the Kelvin sign.- Title case:
ToTitleis not "capitalize each word". The olderstrings.Titleis deprecated because it mishandles apostrophes and punctuation. For proper headings usegolang.org/x/text/cases. - Capitalizing the first letter with
s[:1]only works for ASCII first letters; for multi-byte first runes decode withutf8.DecodeRuneInString. - Map keys: normalize with
ToLoweronce on the way in, not on each lookup. - ASCII-only data:
ToUpperhas a fast path for pure ASCII input. - Bytes:
bytes.ToUpperandbytes.ToLowerdo the same for[]byte.