11

I have found rune type in Go and have a simple question but worth an explnation.

I fount that it is an alias for int32 and purpose is to distinguish number and character values.

http://golang.org/pkg/builtin/#rune

But I am confused with the term "rune" what actually it stands for ? e.g uint == unsigned int

nish1013
  • 3,658
  • 8
  • 33
  • 46

2 Answers2

11

But I am confused with the term "rune" what actually it stands for ? e.g uint == unsigned int

Rune stands for letter. ("Runes" are the letters in a set of related alphabets known as runic alphabets, which were used to write various Germanic languages before the adoption of the Latin alphabet. [Wikipedia]).

If a variable has type rune in Go you know it is intended to hold a unicode code point. (rune is shorter and clearer than codepoint). But it is technical a int32, i.e. its representation in memory is that of an int32.

Volker
  • 40,468
  • 7
  • 81
  • 87
  • http://en.wikipedia.org/wiki/Runes - ideal for Lord of the Rings fans :-P And the old English Þ thorn letter, still in use in Iceland, has a runic counterpart. – Rick-777 Jul 25 '13 at 20:26
  • But there are codepoints for non-Runic alphabets (e.g. the CJK extensions: http://unicode.org/charts/). To call a 32-bit codepoint a "rune" seems quite misleading to me. – aschmied Dec 04 '17 at 15:07
6

In the general sense, Unicode "rune" is just a number, exactly like 64(0x40) is the number which is the code for '@' in both ASCII and Unicode.

  • Is 64 a real number? Yes, of course. you can assign literal 64 to a float variable.
  • Is 64 an integral number? Yes. You can assign literal 64 to any integral variable.
  • Is 64 a signed number? Yes. You can assing literal 64 to any unsigned variable.
  • Is 64 an unsigned number? Yes. You can assign literal 64 to any signed variable.

package main

import "fmt"

func main() {
    var f float64
    f = 64
    var b int8
    b = 64
    var u uint16
    u = 64
    var i int
    i = 64
    fmt.Println(f, b, u, i)

}

Playground


Output:

64 64 64 64

What this attempts to show is that [small] whole numbers (as well as such literals) are basically typeless, i.e. untyped.

Related: Rune Literals.

zzzz
  • 87,403
  • 16
  • 175
  • 139