strings.ToLower() method in Golang

ToLower() is used to convert the Unicode letters of a string to their lowercase values. In this post, we are going to discuss the ToLower() method of the strings package in detail.

  • Method declaration – func ToLower(s string) string
  • What does it do? It will convert all of the Unicode letters of the string passed in the string and map them to their lowercase versions. It will check for all of the letters in the string and convert only those which fall in the range between ‘A’-‘Z’, while all others will be ignored and kept as it is.
  • What does it return? It will return a string after converting all of the letters of the original string into the lowercase.
Code Example
package main

import (
	"fmt"
	"strings"
)

func main() {

	string1 := "**Hi CODEKRU**"
	string2 := "LeaRnINg GoLang"
	string3 := "lowercase string"

	fmt.Println("Original Strings: ")
	fmt.Println(string1)
	fmt.Println(string2)
	fmt.Println(string3)

	string1 = strings.ToLower(string1)
	string2 = strings.ToLower(string2)
	string3 = strings.ToLower(string3)

	fmt.Println()
	fmt.Println("After using ToLower() method: ")
	fmt.Println(string1)
	fmt.Println(string2)
	fmt.Println(string3)

}

Output –

Original Strings: 
**Hi CODEKRU**
LeaRnINg GoLang
lowercase string

After using ToLower() method: 
**hi codekru**
learning golang
lowercase string
What if we use an uninitialized string with ToLower()?

An uninitialized string in Golang is an empty string. So, it will be like passing an empty string in the function and thus we will get the empty string back.

package main

import (
	"fmt"
	"strings"
)

func main() {

	var string1 string

	fmt.Println("lowercase string:", strings.ToLower(string1))

}

Output –

lowercase string: 

If you want to convert your string into an uppercase one, then you can use the ToUpper() method of the strings package.

Please visit this link if you want to know more about the Strings in Go language and its other functions or methods.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *