Strings and Runes in Go

Go: Strings and Runes

In Go, strings are essentially sequences of bytes. However, sometimes we need to work with individual Unicode characters in a string, rather than just individual bytes. This is where the concept of "string runes" comes in.

A rune is a Unicode code point, which is an integer value that represents a single character in a string. In Go, a rune is represented using the rune data type, which is an alias for the int32 data type.

How to work with string runes in Go

To work with string runes in Go, we can use the for range loop, which iterates over the Unicode code points of a string. For example:

    package main

    import "fmt"

    func main() {
        str := "Hello, 世界"
        for _, r := range str {
            fmt.Printf("%c", r)
        }
    }

Explainations: we define a string str that contains the text "Hello, 世界". We then use the for range loop to iterate over the Unicode code points of the string. The loop assigns each code point to the variable r, which is of type rune. We can then print out each code point using the %c format specifier, which tells fmt.Printf() to print the code point as a Unicode character.

The output of this program would be:

    Hello, 世界

Note that in the for range loop, we use the _ identifier to discard the index of each code point, since we don't need it in this case. If we did need the index, we could use a regular for loop instead and use the len() and []rune functions to access individual runes in the string.

Previous Article

Next Article