Convert a String to Lowercase in Go
To convert a string to lowercase in Go, import the strings package and pass the string to strings.ToLower(). The function returns a new string in which Unicode letters are mapped to lowercase.
The original string is not modified because Go strings are immutable. Store the returned value in a variable when you need to use the lowercase result.
Go strings.ToLower() Syntax
The syntax of ToLower() function is
strings.ToLower(str)
where
stringsis the package.ToLoweris the function name.stris the input string.
strings.ToLower() accepts a string and returns its lowercase form. Characters that do not have a lowercase mapping, including digits, spaces, and most punctuation marks, remain unchanged.
Convert Mixed-Case Text to Lowercase in Go
In the following program, we will take a string str containing a mix of lower and upper case alphabets and convert it to lower case.
example.go
package main
import (
"fmt"
"strings"
)
func main() {
var str = "HelLo WoRld"
var str_lower = strings.ToLower(str)
fmt.Printf(str_lower)
}
Output
hello world
The uppercase letters in HelLo WoRld are converted to lowercase, while the space remains unchanged.
Lowercase a String Read from User Input
You can apply strings.ToLower() to a value read at runtime. The following example reads a complete line, removes surrounding whitespace, and prints the lowercase result.
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter text: ")
text, _ := reader.ReadString('\n')
text = strings.TrimSpace(text)
lowercaseText := strings.ToLower(text)
fmt.Println("Lowercase:", lowercaseText)
}
For an input such as GO Programming 101!, the program produces the following result.
Enter text: GO Programming 101!
Lowercase: go programming 101!
How strings.ToLower() Handles Unicode Text
strings.ToLower() works with UTF-8 strings and uses Unicode case mappings. It is therefore preferable to manually changing ASCII byte values when the input may contain non-ASCII letters.
package main
import (
"fmt"
"strings"
)
func main() {
text := "CAFÉ ÜBER"
fmt.Println(strings.ToLower(text))
}
Output
café über
Unicode case conversion can be more complex than replacing the bytes for A through Z. Use the standard library unless your program intentionally supports ASCII-only data.
Confirm That the Original Go String Is Unchanged
Calling strings.ToLower() does not update the source variable automatically. The returned string must be assigned to another variable or back to the same variable.
package main
import (
"fmt"
"strings"
)
func main() {
original := "Go Language"
lowercase := strings.ToLower(original)
fmt.Println("Original:", original)
fmt.Println("Lowercase:", lowercase)
}
Output
Original: Go Language
Lowercase: go language
Use Lowercase Strings for Case-Insensitive Comparisons
One common use of lowercase conversion is normalizing two values before comparing them. This approach is useful when you also need the normalized values for storage, indexing, or later processing.
package main
import (
"fmt"
"strings"
)
func main() {
first := "GOLANG"
second := "golang"
matches := strings.ToLower(first) == strings.ToLower(second)
fmt.Println(matches)
}
Output
true
When you only need a case-insensitive equality check, strings.EqualFold() expresses that intention directly and avoids creating lowercase copies solely for comparison.
matches := strings.EqualFold("GOLANG", "golang")
Convert a Go String to Uppercase Instead
Use strings.ToUpper() when the required result is uppercase rather than lowercase. It has the same basic calling pattern.
uppercase := strings.ToUpper(str)
Common Mistakes When Lowercasing Go Strings
- Forgetting the import: Add
"strings"to the import block before callingstrings.ToLower(). - Ignoring the returned value: The function does not modify the original string. Assign its result to a variable.
- Changing bytes manually: ASCII arithmetic does not correctly handle general Unicode text.
- Lowercasing only for equality: Consider
strings.EqualFold()when you only need a case-insensitive comparison. - Expecting punctuation removal:
ToLower()changes letter case; it does not trim spaces or remove punctuation.
Go String Lowercase FAQs
Which Go package provides ToLower()?
The strings package provides ToLower(). Import it with import "strings".
Does strings.ToLower() modify the original string?
No. Go strings are immutable. strings.ToLower() returns a lowercase string, which you must assign to a variable.
Does strings.ToLower() support Unicode characters?
Yes. It uses Unicode lowercase mappings rather than limiting conversion to ASCII letters.
How do I compare two Go strings without case sensitivity?
Use strings.EqualFold(first, second) when you only need to test whether two strings are equal while ignoring case. Convert both strings with strings.ToLower() when you also need their normalized lowercase values.
Does ToLower() remove spaces or punctuation?
No. It converts letters to lowercase but leaves spaces, digits, and punctuation in the string. Use functions such as strings.TrimSpace() separately when whitespace also needs to be removed.
Editorial QA Checklist for Go Lowercase Examples
- Confirm that every runnable example imports the
stringspackage. - Verify that the returned value from
strings.ToLower()is assigned or printed. - Check that output blocks match the letter case produced by the example code.
- Keep Unicode behavior distinct from ASCII-only byte conversion.
- Use
strings.EqualFold()only for case-insensitive equality, not as a replacement when a lowercase value is required.
Summary of Go String Lowercase Conversion
Use strings.ToLower(str) to obtain the lowercase form of a Go string. The function supports Unicode case mappings, preserves non-letter characters, and returns a new string without changing the original value. The complete API is documented in the Go strings package reference.
In this Go Tutorial, we learned how to convert a string to lower case using strings.ToLower() function.
TutorialKart.com