Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a rune?

Tags:

go

rune

Rune literals are just 32-bit integer values (however they're untyped constants, so their type can change). They represent unicode codepoints. For example, the rune literal 'a' is actually the number 97.

Therefore your program is pretty much equivalent to:

package main

import "fmt"

func SwapRune(r rune) rune {
    switch {
    case 97 <= r && r <= 122:
        return r - 32
    case 65 <= r && r <= 90:
        return r + 32
    default:
        return r
    }
}

func main() {
    fmt.Println(SwapRune('a'))
}

It should be obvious, if you were to look at the Unicode mapping, which is identical to ASCII in that range. Furthermore, 32 is in fact the offset between the uppercase and lowercase codepoint of the character. So by adding 32 to 'A', you get 'a' and vice versa.


From the Go lang release notes: http://golang.org/doc/go1#rune

Rune is a Type. It occupies 32bit and is meant to represent a Unicode CodePoint. As an analogy the english characters set encoded in 'ASCII' has 128 code points. Thus is able to fit inside a byte (8bit). From this (erroneous) assumption C treated characters as 'bytes' char, and 'strings' as a 'sequence of characters' char*.

But guess what. There are many other symbols invented by humans other than the 'abcde..' symbols. And there are so many that we need 32 bit to encode them.

In golang then a string is a sequence of bytes. However, since multiple bytes can represent a rune code-point, a string value can also contain runes. So, it can be converted to a []rune, or vice versa.

The unicode package http://golang.org/pkg/unicode/ can give a taste of the richness of the challenge.


I have tried to keep my language simple so that a layman understands rune.

A rune is a character. That's it.

It is a single character. It's a character from any alphabet from any language from anywhere in the world.

To get a string we use

double-quotes ""

OR

back-ticks ``

A string is different than a rune. In runes we use

single-quotes ''

Now a rune is also an alias for int32...Uh What?

The reason rune is an alias for int32 is because we see that with coding schemes such as below enter image description here

each character maps to some number and so it's the number that we are storing. For example, a maps to 97 and when we store that number it's just the number and so that's way rune is an alias for int32. But is not just any number. It is a number with 32 'zeros and ones' or '4' bytes. (Note: UTF-8 is a 4-byte encoding scheme)

How runes relate to strings?

A string is a collection of runes. In the following code:

    package main

    import (
        "fmt"
    )

    func main() {
        fmt.Println([]byte("Hello"))
    }

We try to convert a string to a stream of bytes. The output is:

[72 101 108 108 111]

We can see that each of the bytes that makes up that string is a rune.


I do not have enough reputation to post a comment to fabrizioM's answer, so I will have to post it here instead.

Fabrizio's answer is largely correct, and he certainly captured the essence of the problem - though there is a distinction which must be made.

A string is NOT necessarily a sequence of runes. It is a wrapper over a 'slice of bytes', a slice being a wrapper over a Go array. What difference does this make?

A rune type is necessarily a 32-bit value, meaning a sequence of values of rune types would necessarily have some number of bits x*32. Strings, being a sequence of bytes, instead have a length of x*8 bits. If all strings were actually in Unicode, this difference would have no impact. Since strings are slices of bytes, however, Go can use ASCII or any other arbitrary byte encoding.

String literals, however, are required to be written into the source encoded in UTF-8.

Source of information: http://blog.golang.org/strings


(Got a feeling that the above answers still didn't state the differences & relationships between string and []rune very clearly, so I would try to add another answer with an example.)

As @Strangework's answer said, string and []rune are quite different.

Differences - string & []rune:

  • string value is a read-only byte slice. And, a string literal is encoded in utf-8. Each char in string actually takes 1 ~ 3 bytes, while each rune takes 4 bytes
  • For string, both len() and index are based on bytes.
  • For []rune, both len() and index are based on rune (or int32).

Relationships - string & []rune:

  • When you convert from string to []rune, each utf-8 char in that string becomes a rune.
  • Similarly, in the reverse conversion, when converting from []rune to string, each rune becomes a utf-8 char in the string.

Tips:

  • You can convert between string and []rune, but still they are different, in both type & overall size.

(I would add an example to show that more clearly.)


Code

string_rune_compare.go:

// string & rune compare,
package main

import "fmt"

// string & rune compare,
func stringAndRuneCompare() {
    // string,
    s := "hello你好"

    fmt.Printf("%s, type: %T, len: %d\n", s, s, len(s))
    fmt.Printf("s[%d]: %v, type: %T\n", 0, s[0], s[0])
    li := len(s) - 1 // last index,
    fmt.Printf("s[%d]: %v, type: %T\n\n", li, s[li], s[li])

    // []rune
    rs := []rune(s)
    fmt.Printf("%v, type: %T, len: %d\n", rs, rs, len(rs))
}

func main() {
    stringAndRuneCompare()
}

Execute:

go run string_rune_compare.go

Output:

hello你好, type: string, len: 11
s[0]: 104, type: uint8
s[10]: 189, type: uint8

[104 101 108 108 111 20320 22909], type: []int32, len: 7

Explanation:

  • The string hello你好 has length 11, because the first 5 chars each take 1 byte only, while the last 2 Chinese chars each take 3 bytes.

    • Thus, total bytes = 5 * 1 + 2 * 3 = 11
    • Since len() on string is based on bytes, thus the first line printed len: 11
    • Since index on string is also based on bytes, thus the following 2 lines print values of type uint8 (since byte is an alias type of uint8, in go).
  • When converting the string to []rune, it found 7 utf8 chars, thus 7 runes.

    • Since len() on []rune is based on rune, thus the last line printed len: 7.
    • If you operate []rune via index, it will access base on rune.
      Since each rune is from a utf8 char in the original string, thus you can also say both len() and index operation on []rune are based on utf8 chars.