Table of contents
1.
Introduction
2.
Rune in Golang
3.
Encoding method
4.
Declaration
5.
Character vs. rune
6.
Important Points to Consider
7.
Frequently Asked Questions
7.1.
What is rune in programming?
7.2.
What is the difference between string and rune in Go?
8.
What is the rune code point in Golang?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

Rune in Golang

Author APURV RATHORE
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Welcome to our blog on Rune in Golang! In this blog, we will discuss the fundamentals of Golang: Runes. If you're curious about what exactly a Rune is in Golang, you've come to the right place. Let's explore this concept more in detail.

What is Rune in Golang?

Runes unlocks powerful capabilities for handling Unicode characters in your Go programs. In this blog, we'll break down what exactly a Rune is, why it matters, and how you can leverage it to enhance your Golang projects. 

Rune in Golang

Rune literals are indeed just 32-bit integers. The Unicode codepoints are represented by them. The rune literal 'a', for example, is number 97.

The rune constant is represented by a rune literal, which is an integer value that recognises the Unicode code point.

The rune is represented in Go by one or more characters contained in single quotes, such as 'g,' '\t,' and so on. You can put any character between single quotes except a newline and an unescaped single quote. These single-quoted characters reflect the Unicode value of the provided character, while multi-character sequences beginning with a backslash (at the start of the multi-character sequence) encode values in a different format.

Encoding method

In Golang, Runes are used to represent Unicode characters. Each Rune is a 32-bit integer representing a single Unicode code point. The encoding method in Golang for Runes ensures that characters from various languages and scripts can be efficiently handled within programs. Golang provides built-in functions and methods to work with Runes, enabling developers to manipulate and process Unicode text effectively.

Declaration

In Golang, Runes are declared using the rune keyword. For example, var r rune = 'a'. This declares a variable r of type rune, initialized with the Unicode code point for the lowercase letter 'a'. By using the rune type, Golang ensures proper handling of Unicode characters, allowing developers to work with text in a flexible and language-independent manner.

Character vs. rune

In Golang, a "character" typically refers to a single UTF-8 encoded byte, which may represent a part of a Unicode character or an ASCII character. On the other hand, a "rune" refers to a Unicode code point, represented by a 32-bit integer. While a character in Golang can be of variable length due to UTF-8 encoding, a rune always represents a single Unicode code point, providing a more consistent and reliable way to work with Unicode text in Golang programs.

Important Points to Consider

  • Bytes make up strings, which can include valid characters that can be represented with runes.
  • To convert a string to an array of runes, we use the rune() method.
  • Always keep in mind that a string is a collection of bytes, not a rune. However, a string could contain Unicode content encoded in UTF-8, and because the go source code always encodes in UTF-8, there is no need to encode the string in UTF-8.
  • UTF-8 encodes all Unicode characters in 1 to 4 bytes, with 1 byte for ASCII and the remainder for the rune.
  • ASCII has a total of 256 characters. In which 128 is the number of characters and 0-127 is the number of code points. The element that represents a single value is referred to as a code point.
package main

import "fmt"

func main() {
   chr := 'z'
   chrRune := rune(chr)
   fmt.Println("The rune")
   fmt.Println(chrRune)
}

 

Output:

The rune
122

 

Now let us see how to convert string into array of runes. 

package main
import "fmt"

func main() {

   strg := "ThisIsAString"
   strRuneArray := []rune(strg)
   fmt.Println("The rune array")
   fmt.Println(strRuneArray)

}

 

Output:

The rune array
[84 104 105 115 73 115 65 83 116 114 105 110 103]

 

Here is another example. 

package main
import (
   "fmt"
   "reflect"
)

func main() {

   runeOne := 'd'
   runeTwo := 'q'

   fmt.Printf("Unicode: %U; Type: %s", runeOne, reflect.TypeOf(runeOne))
   fmt.Printf("\nUnicode: %U; Type: %s", runeTwo, reflect.TypeOf(runeTwo))

}

 

Output:

Unicode: U+0064; Type: int32
Unicode: U+0071; Type: int32

Frequently Asked Questions

What is rune in programming?

Rune represents a Unicode code point in programming, typically a 32-bit integer, used for handling characters and text.

What is the difference between string and rune in Go?

Strings are a sequence of bytes, while runes represent Unicode code points, ensuring proper handling of characters.

What is the rune code point in Golang?

Rune code points in Golang are 32-bit integers representing Unicode characters, facilitating language-independent text processing.

Conclusion

In this article, we have extensively discussed runes and their implementation in the Golang programming language. We hope that this blog has helped you enhance your knowledge regarding runes and if you would like to learn more, check out our articles on GoLang. Do upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass