How to Convert a String in Uppercase in Golang?

Last updated on March 7, 2023

Golang provides a ToUpper() function that converts strings into uppercase. For example if you pass a string that contains lower alphabets “abc” into ToUpper() function then you will get alphabets in uppercase (“ABC”) in the response.

Syntax

strings.ToUpper(any string)

Parameters

  • It takes one required parameter.
  • It accepts string value in the argument.

Return Value

It returns an uppercase string.

Usage

You need to import the strings package into your go file for using the ToUpper() function. So add below script in the file to import strings package.

import (
	"fmt"
	"strings"
)

This script imports fmt and strings packages.

fmt.Println(strings.ToUpper("programming"))
// Output: PROGRAMMING

In this example, we passed a string into the ToUpper() function that converted the programming word into uppercase.

In the above example, we passed a single word. Now let’s pass a phrase to check if a function works with a phrase or not.

fmt.Println(strings.ToUpper("programming is easy"))
// Output: PROGRAMMING IS EASY

As you can see we got uppercase based strings in the response. It means the ToUpper() function works with both a single word and phrase.

Conclusion

This article demonstrates how you can convert strings in uppercase. To do that it uses the ToUpper() function from the strings package.


Written by
I am a skilled full-stack developer with extensive experience in creating and deploying large and small-scale applications. My expertise spans front-end and back-end technologies, along with database management and server-side programming.

Share on:

Related Posts

Tags: Golang, String,