0
0
GoHow-ToBeginner · 2 min read

Go Program to Convert String to Uppercase

In Go, use strings.ToUpper(yourString) to convert a string to uppercase. Import the strings package and call ToUpper with your string as argument.
📋

Examples

Inputhello
OutputHELLO
InputGoLang
OutputGOLANG
Input123abc!
Output123ABC!
🧠

How to Think About It

To convert a string to uppercase in Go, think of changing each letter to its capital form. The strings package has a ready function ToUpper that does this for the whole string at once, so you just need to pass your string to it.
📐

Algorithm

1
Import the strings package.
2
Get the input string you want to convert.
3
Call strings.ToUpper with the input string.
4
Return or print the uppercase string.
💻

Code

go
package main

import (
	"fmt"
	"strings"
)

func main() {
	input := "hello Go"
	upper := strings.ToUpper(input)
	fmt.Println(upper)
}
Output
HELLO GO
🔍

Dry Run

Let's trace converting "hello Go" to uppercase using strings.ToUpper.

1

Input string

input = "hello Go"

2

Convert to uppercase

upper = strings.ToUpper(input) // upper = "HELLO GO"

3

Print result

Print upper -> "HELLO GO"

StepValue
Inputhello Go
UppercaseHELLO GO
💡

Why This Works

Step 1: Import strings package

The strings package provides useful string functions including ToUpper.

Step 2: Use ToUpper function

ToUpper takes a string and returns a new string with all letters converted to uppercase.

Step 3: Print the result

The uppercase string is printed to show the conversion result.

🔄

Alternative Approaches

Manual conversion using rune loop
go
package main

import (
	"fmt"
)

func toUpperManual(s string) string {
	result := []rune{}
	for _, r := range s {
		if r >= 'a' && r <= 'z' {
			r -= 'a' - 'A'
		}
		result = append(result, r)
	}
	return string(result)
}

func main() {
	fmt.Println(toUpperManual("hello Go"))
}
This method manually converts each letter but is longer and less efficient than strings.ToUpper.

Complexity: O(n) time, O(n) space

Time Complexity

The function processes each character once, so time grows linearly with string length.

Space Complexity

A new string is created for the uppercase result, so space also grows linearly.

Which Approach is Fastest?

strings.ToUpper is optimized and faster than manual loops for uppercase conversion.

ApproachTimeSpaceBest For
strings.ToUpperO(n)O(n)Simple, fast uppercase conversion
Manual rune loopO(n)O(n)Learning purpose, custom logic
💡
Always import strings and use strings.ToUpper for easy and reliable uppercase conversion.
⚠️
Forgetting to import the strings package causes a compile error when calling ToUpper.