How to convert a string to uppercase in Go

The strings package is a Go standard library package that contains functions to manipulate UTF-8 encoded strings. It provides the ToUpper() method, which can convert a string to upper case.

This returns a copy of the string input where all the Unicode characters are mapped to its uppercase variant.

Syntax

func ToUpper(s string) string
  • This method takes the string s as input.
  • It returns a string which is the uppercase of the input string.
Converting a string to uppercase using ToUpper()

Code

  • We first imported the fmt and strings package to our program in the code below.

  • We then call the ToUpper() method with house of cards as the input string. The method returns HOUSE OF CARDS as it returns a new string after converting it to uppercase.

  • We then call the ToUpper() method with Game Of Thrones as the input string. The method returns GAME OF THRONES after converting the input to uppercase.

Below is the output of all these operations using the Println() method of the fmt package:

package main
import (
"fmt"
"strings"
)
func main() {
str1 := "house of cards"
str2 := "Game Of Thrones"
fmt.Println("Initial Strings")
fmt.Println(str1);
fmt.Println(str2);
fmt.Println("\nAfter Calling ToUpper()")
fmt.Println(strings.ToUpper(str1))
fmt.Println(strings.ToUpper(str2))
}