符文类型说明

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

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.

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.