And like any other data type, there are some common tasks and operations that arise more times than not when dealing with strings.
One such task is converting the casing of a given string. For example, converting a string to lowercase is very common.
In this guide, we will look at all the methods and techniques that we can use to convert a string into lowercase.
What Are Strings?
In Go, a string is a sequence of Unicode characters that are enclosed within the double quotes. For example:
It is good to keep in mind that the strings are immutable which means that we cannot change its value once created.
Hence, to make the modifications to a string such as converting it to lowercase involves creating a new string with new changes.
Convert a String to Lowercase in Golang
Let us explore the various methods that we can use to convert a string into lowercase:
Method 1: Using the Strings.ToLower Function
One of the easiest and common way of converting a string to lowercase in Go is using the ToLower() function from the strings.
The “strings” package is part of the Go standard library and comes packed with tons of methods and features to work with string types in Go.
The function converts all the characters in a string to lowercase and returns the result as a new string.
The function usage is as shown in the following example:
import (
"fmt"
"strings"
)
func main() {
str := "DEVELOPMENT.LOCAL:3306"
lowerStr := strings.ToLower(str)
fmt.Println(lowerStr)
}
In this case, the given code should return the output as follows:
The ToLower() function provides a more simplistic and effective way of converting a string to lowercase and works well in most cases.
Method 2: Using the Strings.Map Function
The “strings” package also provides a “Map” function that we can use to apply a custom mapping function to each character in a string.
We can use this function to implement a custom lowercase conversion.
An example code is as follows:
import (
"fmt"
"strings"
"unicode"
)
func main() {
str := "DEVELOPMENT.LOCAL:3306"
lowerStr := strings.Map(unicode.ToLower, str)
fmt.Println(lowerStr)
}
In this case, we use the unicode.ToLower() function to convert the characters into lowercase. We then use the “Map” function to apply it to the string.
Method 3: Using a Rune Slice
Another method of converting a string to lowercase is by treating it as a slice of runes (Unicode characters).
We can iterate through the rune slice and convert each character to lowercase as shown in the following example:
import (
"fmt"
"unicode"
)
func main() {
str := "DEVELOPMENT.LOCAL:3306"
runes := []rune(str)
for i, char := range runes {
runes[i] = unicode.ToLower(char)
}
lowerStr := string(runes)
fmt.Println(lowerStr)
}
In this example, we first convert the string into a slice of runes. We then iterate through the slice and convert each character to lowercase.
Finally, we reconstruct the new string from the modified rune slice to get the new lowercased string.
Method 4: Using the Bytes.ToLower Function
We can also use the “bytes” package in Go to convert a string to lowercase. This method works by converting the string into a byte slice, converting them to lowercase, and then converting them back into a string.
import (
"bytes"
"fmt"
)
func main() {
str := "DEVELOPMENT.LOCAL:3306"
lowerStr := string(bytes.ToLower([]byte(str)))
fmt.Println(lowerStr)
}
A good use of this method is when you need to perform a case-insensitive comparison.
Conclusion
In this tutorial, we learned all the methods and techniques that we can use to convert a string into lowercase using the Go programming language.