Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go rune type explanation

Tags:

go

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

like image 231
nish1013 Avatar asked Jul 25 '13 10:07

nish1013


People also ask

What is rune datatype in Golang?

Golang has integer types called byte and rune that are aliases for uint8 and int32 data types, respectively. In Go, the byte and rune data types are used to distinguish characters from integer values. In Go, there is no char data type. It uses byte and rune to represent character values.

What is a rune in programming?

A rune is a type meant to represent a Unicode code point. The rune type is an alias for int32 , and is used to emphasize than an integer represents a code point. ASCII defines 128 characters, identified by the code points 0–127. It covers English letters, Latin numbers, and a few other characters.

What are Golang code points?

Code points, characters, and runes The Unicode standard uses the term “code point” to refer to the item represented by a single value. The code point U+2318, with hexadecimal value 2318, represents the symbol ⌘.

What is the alias of rune datatype?

Go Rune Type The type rune is an alias for type int32. The rune literals are an integer value. If you store string value in rune literal, it will provide the ASCII value of the character. For example, the rune literal of 'A' will be 65.


2 Answers

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.

like image 83
Volker Avatar answered Oct 18 '22 16:10

Volker


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.

like image 32
zzzz Avatar answered Oct 18 '22 16:10

zzzz