What is rune type in Go?

In Go, a rune is an alias for the int32 type. It is used to represent a Unicode code point, which is a unique numerical value assigned to each character in the Unicode standard. A rune is a 32-bit integer that corresponds to a single Unicode character.

Go uses runes to handle Unicode characters and strings efficiently. Since some characters may require more than one byte to represent, using rune allows Go to handle them properly and maintain compatibility with Unicode.

To declare a variable of type rune, you can use the rune keyword or the int32 type:

var r rune
var ch int32

You can assign a Unicode code point (integer value) to a variable of type rune, like this:

var heart rune = 9829 // Unicode code point for heart symbol

Since Go treats strings as sequences of rune values, you can use loops to iterate through the individual runes in a string, as each character in a Go string is represented by a rune.

Here’s an example of using rune to loop through the characters of a string:

package main

import "fmt"

func main() {
    text := "Hello, 世界" // Contains English and Chinese characters

    for _, r := range text {
        fmt.Printf("%c ", r)


H e l l o ,   世 界

In this example, the for range loop iterates over each rune in the string text, printing each character to the console.

Using rune in Go allows you to handle and manipulate Unicode characters efficiently, making it a powerful feature for working with multilingual and internationalized applications.

error: Content is protected !!