Open In App

How to convert a string in lower case in Golang?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding.
In Go string, you are allowed to convert a string in the lowercase using ToLower() function. This function returns a copy of the given string in which all the Unicode letters mapped into lower case. This function is defined under strings package, so you have to import strings package in your program to access this function.

Syntax:

func ToLower(str string) string

Here, str represents a string which you want to convert to lowercase.

Example:




// Go program to illustrate how to convert
// the given string to lowercase
package main
  
import (
    "fmt"
    "strings"
)
  
// Main method
func main() {
  
    // Creating and initializing string
    // Using shorthand declaration
    str1 := "WelcomE, GeeksforGeeks**"
    str2 := "$$This is the, tuTorial oF Golang##"
    str3 := "HELLO! GOLANG"
    str4 := "lowercase conversion"
  
    // Displaying strings
    fmt.Println("Strings (before):")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2:", str2)
    fmt.Println("String 3:", str3)
    fmt.Println("String 4:", str4)
  
    // Converting all the string into lowercase
    // Using ToLower() function
    res1 := strings.ToLower(str1)
    res2 := strings.ToLower(str2)
    res3 := strings.ToLower(str3)
    res4 := strings.ToLower(str4)
  
    // Displaying the results
    fmt.Println("\nStrings (after):")
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2:", res2)
    fmt.Println("Result 3:", res3)
    fmt.Println("Result 4:", res4)
}


Output:

Strings (before):
String 1:  WelcomE, GeeksforGeeks**
String 2: $$This is the, tuTorial oF Golang##
String 3: HELLO! GOLANG
String 4: lowercase conversion

Strings (after):
Result 1:  welcome, geeksforgeeks**
Result 2: $$this is the, tutorial of golang##
Result 3: hello! golang
Result 4: lowercase conversion


Last Updated : 26 Aug, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads