Home
Go
strings.ToLower, ToUpper Examples
Updated Dec 16, 2021
Dot Net Perls
ToLower, ToUpper. There are lowercase and uppercase letters. To change from one to the other, we can use functions from the strings package in Go.
Simple methods. The strings.ToLower and ToUpper funcs have the expected results. They do not change characters like digits, spaces, or punctuation. Title() capitalizes all words.
ToLower example. Let us begin with strings.ToLower. We have a string literal with the value of "Name." When we call ToLower, a new string is returned.
And The uppercase character in the string was changed to a lowercase one—but only in the copied, returned string.
Tip The original string (value) is left alone after strings.ToLower returns.
Result The before and after values are printed with fmt.Println. The string was lowercased.
fmt
package main import ( "fmt" "strings" ) func main() { value := "Name" // Convert to lowercase. valueLower := strings.ToLower(value) fmt.Println("BEFORE:", value) fmt.Println("AFTER: ", valueLower) }
BEFORE: Name AFTER: name
ToUpper example. The ToUpper func works in the same way as ToLower. It returns a new, copied version of the string argument that has lowercase chars changed to uppercase ones.
package main import ( "fmt" "strings" ) func main() { value := "Dot Net Perls" // Use ToUpper. valueUpper := strings.ToUpper(value) fmt.Println("BEFORE:", value) fmt.Println("AFTER: ", valueUpper) }
BEFORE: Dot Net Perls AFTER: DOT NET PERLS
Title. The strings package provides ways to change the case of characters. Here we apply title-casing to a string with the Title func. The result is a properly-capitalized string.
package main import ( "fmt" "strings" ) func main() { name := "the apology" // Capitalize this string. result := strings.Title(name) fmt.Println(result) }
The Apology
A summary. For real-world programs in Go, we often need methods like strings.ToLower and ToUpper. These can help us simplify our programs (as by using only lowercase-based logic).
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 16, 2021 (edit).
Home
Changes
© 2007-2025 Sam Allen