Rabi Siddique
238 words
1 minutes
Runes in Golang
2024-06-12

In Go, a rune is a data type that represents a single Unicode code point. Unicode is a standard for encoding text in most of the world’s writing systems, and each character or symbol in Unicode is assigned a unique code point.

Rune Type#

The rune type in Go is an alias for the int32 type. This means that a rune is essentially a 32-bit integer value that can hold any Unicode code point.

type rune = int32

Using rune instead of int32 helps to clarify the purpose of the value, indicating that it is intended to represent a character rather than a numeric value.

Working with Runes#


package main

import (
    "fmt"
)

func main() {
    // A string containing Unicode characters
    str := "Hello, ربیع صدیق"

    // Convert the string to a slice of runes
    runes := []rune(str)

    // Print each rune and its Unicode code point
    for i, r := range runes {
        fmt.Printf("Character %d: %c (Unicode: %U)\n", i, r, r)
    }
}

Output#

Character 0: H (Unicode: U+0048)
Character 1: e (Unicode: U+0065)
Character 2: l (Unicode: U+006C)
Character 3: l (Unicode: U+006C)
Character 4: o (Unicode: U+006F)
Character 5: , (Unicode: U+002C)
Character 6:   (Unicode: U+0020)
Character 7: ر (Unicode: U+0631)
Character 8: ب (Unicode: U+0628)
Character 9: ی (Unicode: U+06CC)
Character 10: ع (Unicode: U+0639)
Character 11:   (Unicode: U+0020)
Character 12: ص (Unicode: U+0635)
Character 13: د (Unicode: U+062F)
Character 14: ی (Unicode: U+06CC)
Character 15: ق (Unicode: U+0642)
Runes in Golang
https://rabisiddique.com/posts/runes-in-golang/
Author
Rabi Siddique
Published at
2024-06-12