When working with text in GoLang, one of the most common tasks you’ll face is converting string case. Whether you’re building a web application, processing user input, or formatting data for display, changing the case of a string—from uppercase to lowercase or vice versa—is an essential skill. It might seem simple, but string case conversion is a core part of text manipulation in any programming language, and GoLang makes it quite straightforward once you understand how strings work.
with hands-on learning.
get the skills and confidence to land your next move.
In GoLang, strings are immutable, meaning once you create a string, you can’t directly change its contents. Instead, you create a new string with the desired format. Luckily, the Go standard library gives us several ways to do this easily. In this article, we’ll walk through different programs that show how to convert string case in GoLang—from simple built-in functions to more manual approaches like loops and recursion.
By the end of this guide, you’ll not only know how to convert strings between uppercase, lowercase, and title case but also understand how each approach works under the hood. Let’s dive right in.
Program 1: Converting String to Lowercase Using strings.ToLower
This first program shows the easiest way to convert a string to lowercase using GoLang’s built-in strings package.
package main
import (
"fmt"
"strings"
)
func main() {
text := "HELLO FROM ZAMBIA!"
lower := strings.ToLower(text)
fmt.Println("Original:", text)
fmt.Println("Lowercase:", lower)
}In this program, we start by importing the strings package, which provides handy functions for working with text. We then call strings.ToLower() and pass in our string. This function scans each character and converts it to lowercase if possible. The result is printed on the screen.
This method is perfect for beginners because it’s simple, fast, and efficient. You’ll use it often when dealing with user input or standardizing data before storage or comparison.
Program 2: Converting String to Uppercase Using strings.ToUpper
Now, let’s go the other way and change text to uppercase. This method is almost identical to the first one, but instead of ToLower, we use ToUpper.
package main
import (
"fmt"
"strings"
)
func main() {
text := "hello from lusaka"
upper := strings.ToUpper(text)
fmt.Println("Original:", text)
fmt.Println("Uppercase:", upper)
}This program converts every lowercase letter in the string into its uppercase form. The strings.ToUpper() function takes care of all the logic behind the scenes, including special characters and Unicode handling.
Uppercase conversions are common when displaying text in headings, alerts, or standard formats. It’s also useful when comparing strings in a case-insensitive way.
Program 3: Converting String to Title Case Using strings.Title
Sometimes you might want to make the first letter of every word uppercase, like in a book title or a name. GoLang has a function for that too.
package main
import (
"fmt"
"strings"
)
func main() {
text := "welcome to lusaka city"
title := strings.Title(text)
fmt.Println("Original:", text)
fmt.Println("Title Case:", title)
}Here, strings.Title() capitalizes the first letter of each word while keeping the rest in lowercase. This function is especially useful when you want to display names, book titles, or proper nouns neatly. However, note that in newer Go versions, strings.Title() is deprecated. You can achieve the same using cases.Title from the golang.org/x/text package if you’re building larger applications.
First, you need to install the package if you haven’t already. Run the following command in your terminal inside your Go project:
go get golang.org/x/textOnce installed, you can use it in your code like this:
package main
import (
"fmt"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
func main() {
text := "welcome to lusaka city"
// Create a title caser for English
title := cases.Title(language.English).String(text)
fmt.Println("Original:", text)
fmt.Println("Title Case (new version):", title)
}Here’s how it works: you create a cases.Title object and specify the language context (English in this example). Then, calling .String() on your text converts it to title case. This approach is preferred for modern Go applications because it properly handles special characters and works reliably with international text.
Using this method ensures your programs are future-proof and compatible with newer Go versions, while also teaching beginners how to integrate external Go packages in real projects.
Program 4: Manual Conversion to Uppercase Using Loops
Let’s say you want to understand how this actually works behind the scenes. You can manually convert a string to uppercase using a simple loop and rune conversion.
package main
import (
"fmt"
)
func main() {
text := "hello from africa"
var upper string
for _, char := range text {
if char >= 'a' && char <= 'z' {
char = char - 32
}
upper += string(char)
}
fmt.Println("Original:", text)
fmt.Println("Uppercase (manual):", upper)
}In this example, we loop through each character (or rune) in the string. If the character is between ‘a’ and ‘z’, we subtract 32 from its ASCII value, which changes it to uppercase. We then add it to a new string.
This method helps beginners understand how strings and characters are represented in Go. It’s not as efficient as using strings.ToUpper, but it’s a great exercise in learning ASCII values and loops.
Program 5: Recursive Conversion to Lowercase
Recursion is another interesting approach, though not always practical for this kind of task. It’s more of a learning exercise to understand how functions can call themselves.
package main
import (
"fmt"
)
func toLowerRecursive(text string) string {
if text == "" {
return ""
}
char := rune(text[0])
if char >= 'A' && char <= 'Z' {
char = char + 32
}
return string(char) + toLowerRecursive(text[1:])
}
func main() {
text := "HELLO FROM LUSAKA"
lower := toLowerRecursive(text)
fmt.Println("Original:", text)
fmt.Println("Lowercase (recursive):", lower)
}Here, the function checks each character and converts it to lowercase if it’s uppercase. Then it calls itself again for the rest of the string. When the string becomes empty, it stops.
While this approach is more academic than practical, it’s excellent for beginners to understand recursion and function calls in GoLang.
Program 6: Changing Case Using the unicode Package
GoLang also has a unicode package that gives you more control, especially with international characters.
package main
import (
"fmt"
"unicode"
)
func main() {
text := "Hello From Zambia!"
var upper string
for _, r := range text {
upper += string(unicode.ToUpper(r))
}
fmt.Println("Original:", text)
fmt.Println("Unicode Uppercase:", upper)
}This program uses unicode.ToUpper() to handle each rune safely. It’s especially useful when working with text that may include non-English characters. Unlike the ASCII-based approach, this method respects Unicode standards, making it the most reliable way to change case in multilingual applications.
Frequently Asked Questions (FAQ)
This section answers common questions beginners often ask about converting string cases in GoLang.
Q1: Why does GoLang use runes instead of characters?
GoLang uses runes because they can represent Unicode code points. This means you can handle characters from different languages safely without breaking your program.
Q2: Is strings.Title() still recommended for production use?
No. It has been deprecated in newer Go versions. You can use the cases.Title function from the golang.org/x/text/cases package instead.
Q3: Can I mix different case conversions in one program?
Yes, you can easily chain them together. For example, you can use strings.ToLower() first and then strings.Title() to create sentence case formatting.
Q4: Are string conversions in GoLang memory-heavy?
Not really. GoLang handles string operations efficiently, though creating new strings repeatedly can be costly in very large applications. For normal use, it’s perfectly fine.
Conclusion
Converting string case in GoLang is one of those small but essential skills that every programmer needs. From simple transformations using strings.ToLower() and strings.ToUpper() to more hands-on methods using loops, recursion, and the unicode package, GoLang provides multiple ways to achieve the same goal depending on your needs.
As you continue learning, try experimenting with different inputs—names, phrases, and even non-English text—to see how GoLang handles them. Practice often, and soon you’ll find string manipulation to be one of the easiest and most enjoyable parts of Go programming.




