How to Map a Rune to Uppercase in Golang?


Golang, also known as Go, is a powerful programming language that offers efficient and scalable solutions for building modern software. One of the essential features of Golang is the ability to handle Unicode characters, which makes it possible to work with various languages and scripts.

In this article, we'll discuss how to map a rune to uppercase in Golang, and provide a step-by-step guide on how to achieve this task.

What is a Rune in Golang?

A rune in Golang is a Unicode code point, which represents a single character. In Golang, the type for a rune is rune, and it's an alias for int32. Since Go supports Unicode natively, runes can represent any character from any language, including special characters and symbols.

Why Map a Rune to Uppercase?

In many cases, we need to convert a lowercase rune to its uppercase equivalent. For instance, when we're building a text processing application that requires case-insensitive search, or when we need to display the text in uppercase format.

How to Map a Rune to Uppercase in Golang?

In Golang, we can use the unicode package to map a rune to its uppercase equivalent. The unicode package provides several functions that allow us to convert a rune to its uppercase, lowercase, or titlecase equivalent.

The ToUpper() function in the unicode package takes a rune as an argument and returns its uppercase equivalent. Here's an example of how to use the ToUpper() function to map a rune to uppercase −

Example

package main

import (
   "fmt"
   "unicode"
)

func main() {
   r := 'a'
   upper := unicode.ToUpper(r)
   fmt.Printf("%c", upper)
}

Output

A

In the above example, we first declare a rune r with the value 'a'. Then we use the ToUpper() function to convert the lowercase rune to its uppercase equivalent, and store the result in the upper variable. Finally, we use the Printf() function to print the uppercase rune to the console.

Conclusion

In this article, we discussed how to map a rune to uppercase in Golang. We learned that a rune in Golang represents a single Unicode character, and that we can use the unicode package to convert a lowercase rune to its uppercase equivalent. By following the step-by-step guide provided in this article, you should now be able to easily map runes to uppercase in your Golang programs.

Updated on: 08-May-2023

247 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements