How to convert a string to lowercase in Go
The strings package is a Go standard library package that contains functions to manipulate UTF-8 encoded strings.
The ToLower() method
It provides the ToLower() method, which can convert a string to a lower case.
Returns
This returns a copy of the string input where all the Unicode characters are mapped to its lowercase variant.
Syntax
func ToLower(s string) string
Arguments
-
This method takes a string
sas input. -
It returns a string which is the lowercase of the input string.
Code
First, we imported the fmt and strings package to our program in the code below:
package mainimport ("fmt""strings")func main() {str1 := "NewYork"str2 := "CITY OF DREAMS"fmt.Println("Initial Strings")fmt.Println(str1);fmt.Println(str2);fmt.Println("\nAfter Calling ToLower()")fmt.Println(strings.ToLower(str1))fmt.Println(strings.ToLower(str2))}
Explanation
After importing fmt we call the ToLower() method with New York as the input string. This returns new york as it returns a new string after converting it to lowercase.
We then call the ToLower() method with CITY OF DREAMS as the input string. This returns city of dreams after converting the input to lowercase.
We show the output of all these operations using the Println() method of the fmt package.
Output
The program prints below output and exits:
Initial Strings
NewYork
CITY OF DREAMS
After Calling ToLower()
newyork
city of dreams