Convert a String to Uppercase in Go
To convert a string to uppercase in Go, import the strings package and pass the string to strings.ToUpper(). The function returns a new string with its letters mapped to uppercase.
Go strings are immutable, so strings.ToUpper() does not modify the original string. Store or use the value returned by the function.
Go strings.ToUpper() Syntax
The syntax of ToUpper() function is
strings.ToUpper(str)
where
stringsis the package.ToUpperis the function name.stris the input string.
strings.ToUpper() returns a string in which letters are converted to their uppercase form. Digits, spaces, punctuation marks, and other characters without an uppercase mapping remain unchanged.
Convert Mixed-Case Text to Uppercase in Go
In the following program, we will take a string str containing a mix of lower and upper case alphabets and convert it to upper case.
example.go
package main
import (
"fmt"
"strings"
)
func main() {
var str = "HelLo WoRld"
var str_upper = strings.ToUpper(str)
fmt.Printf(str_upper)
}
Output
HELLO WORLD
Every lowercase letter in HelLo WoRld is converted to uppercase. The space between the two words remains unchanged.
Convert User Input to Uppercase in Go
The following example reads a complete line from standard input, removes surrounding whitespace, and converts the entered text to uppercase.
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter text: ")
text, _ := reader.ReadString('\n')
text = strings.TrimSpace(text)
uppercaseText := strings.ToUpper(text)
fmt.Println("Uppercase:", uppercaseText)
}
For an input such as Go Programming 101!, the program prints:
Enter text: Go Programming 101!
Uppercase: GO PROGRAMMING 101!
How strings.ToUpper() Handles Unicode Text
strings.ToUpper() uses Unicode case mappings. It can therefore convert many non-ASCII letters correctly and is safer than manually changing byte values when your text may contain international characters.
package main
import (
"fmt"
"strings"
)
func main() {
text := "café über"
fmt.Println(strings.ToUpper(text))
}
Output
CAFÉ ÜBER
Use the standard library for general text. A byte-by-byte ASCII conversion only handles the letters a through z and is not suitable for arbitrary UTF-8 input.
Verify That ToUpper() Does Not Change the Original String
Because strings are immutable in Go, the source string remains unchanged after calling strings.ToUpper().
package main
import (
"fmt"
"strings"
)
func main() {
original := "Go Language"
uppercase := strings.ToUpper(original)
fmt.Println("Original:", original)
fmt.Println("Uppercase:", uppercase)
}
Output
Original: Go Language
Uppercase: GO LANGUAGE
To replace the value stored in the original variable, assign the result back to it:
str = strings.ToUpper(str)
Convert Only the First Letter of a Go String to Uppercase
strings.ToUpper() converts every letter in the string. When only the first Unicode character should be uppercased, convert the string to a rune slice and use unicode.ToUpper() on the first rune.
package main
import (
"fmt"
"unicode"
)
func uppercaseFirst(text string) string {
runes := []rune(text)
if len(runes) == 0 {
return text
}
runes[0] = unicode.ToUpper(runes[0])
return string(runes)
}
func main() {
fmt.Println(uppercaseFirst("golang tutorial"))
}
Output
Golang tutorial
This example changes only the first rune. It does not convert the remainder of the string to title case.
Use Uppercase Strings in Case-Insensitive Comparisons
Two strings can be normalized with strings.ToUpper() before comparison when the uppercase values are also needed for later processing.
package main
import (
"fmt"
"strings"
)
func main() {
first := "GoLang"
second := "GOLANG"
matches := strings.ToUpper(first) == strings.ToUpper(second)
fmt.Println(matches)
}
Output
true
When you only need to test equality without regard to case, strings.EqualFold() states that intention more directly and avoids creating uppercase copies solely for comparison.
matches := strings.EqualFold("GoLang", "GOLANG")
Convert a Go String to Lowercase Instead
Use strings.ToLower() when the required result is lowercase. It accepts and returns a string in the same way as strings.ToUpper().
lowercase := strings.ToLower(str)
Common Go Uppercase Conversion Mistakes
- Calling the wrong function: Use
strings.ToUpper(), notstrings.ToLower(), when the result must be uppercase. - Forgetting the strings import: Add
"strings"to the import block before callingstrings.ToUpper(). - Ignoring the returned string: The original value is not changed unless the result is assigned back to the variable.
- Using byte arithmetic for Unicode text: Manual ASCII conversion does not handle general UTF-8 strings correctly.
- Using ToUpper() to capitalize one character: The function uppercases the entire string. Handle the first rune separately when only the initial letter should change.
Go String Uppercase FAQs
How do I convert a string to uppercase in Go?
Import the strings package and call strings.ToUpper(value). The function returns the uppercase version of the supplied string.
Is ToUpper() a method on a Go string?
No. Go strings do not provide an upper() method. ToUpper() is a function in the standard library’s strings package.
Does strings.ToUpper() modify the original string?
No. It returns a new string because Go strings are immutable. Assign the returned value to a variable or back to the original variable.
Does strings.ToUpper() support Unicode characters?
Yes. It applies Unicode uppercase mappings, so it can handle many non-ASCII letters in UTF-8 strings.
How do I uppercase only the first letter in Go?
Convert the string to a rune slice, apply unicode.ToUpper() to the first rune, and convert the rune slice back to a string. This avoids corrupting multibyte UTF-8 characters.
Editorial QA Checklist for Go ToUpper() Examples
- Confirm that each runnable uppercase example imports the
stringspackage. - Verify that every call uses
strings.ToUpper()rather thanstrings.ToLower(). - Check that the returned uppercase string is assigned, printed, or otherwise used.
- Ensure output blocks preserve digits, spaces, and punctuation from the input.
- Use rune-based logic rather than byte indexing in examples that uppercase only the first Unicode character.
Summary of Go String Uppercase Conversion
Use strings.ToUpper(str) to convert all applicable letters in a Go string to uppercase. The function returns a new string, supports Unicode case mappings, and leaves characters such as digits and punctuation unchanged. Refer to the Go strings package documentation for the standard library API.
In this Go Tutorial, we learned how to convert a string to upper case using strings.ToUpper() function.
TutorialKart.com