How to convert string to lowercase in Golang

  • 07 May 2019
  • ADM

 

How to convert string to lowercase in Golang - images/logos/golang.jpg

 

Here is a simple snippet of how to convert a string to lowercase using Golang. If you need help how to install Golang check the references links.

Code

To convert a string into lowercase, use the ToLower method from strings package.

package main

import (	
	"fmt"
	"strings"
)

func main() {
	str := "Hello from ADMFactory.com"
	res:= strings.ToLower(str)
	fmt.Println("String to Lowercase Golang example")
	fmt.Println()
	fmt.Println(str + " ==> " + res)
}

Compile&Run

To compile the code navigate to the file location and run the following command.

$ go build stringToLower.go

Then depending on if you are on Linux or Windows the binary file is created.

To run the application execute the command.

Linux

$ ./stringToLower

Windows

c:\Users\adm\go\tutorials> stringToLower.exe

If you want to compile and run the application in one single step run the following command:

go run stringToLower.go

Output

Will display the following output.

String to Lowercase Golang example

Hello from ADMFactory.com ==> hello from admfactory.com

 

References