Skip to content
Tutorials Center
My learning

Topics

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.

Go
strings.ToUpper("go") // "GO"

Try it

Go Changing case

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.

Go Case-insensitive comparison

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

Go
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), and EqualFold("Straße", "STRASSE") is false.
  • 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 the Special variants 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.
  • EqualFold beats ToLower(a) == ToLower(b): no allocation, and it handles characters like the Kelvin sign.
  • Title case: ToTitle is not "capitalize each word". The older strings.Title is deprecated because it mishandles apostrophes and punctuation. For proper headings use golang.org/x/text/cases.
  • Capitalizing the first letter with s[:1] only works for ASCII first letters; for multi-byte first runes decode with utf8.DecodeRuneInString.
  • Map keys: normalize with ToLower once on the way in, not on each lookup.
  • ASCII-only data: ToUpper has a fast path for pure ASCII input.
  • Bytes: bytes.ToUpper and bytes.ToLower do the same for []byte.