Go rune类型解释

huangapple go评论94阅读模式
英文:

Go rune type explanation

问题

我在Go语言中找到了rune类型,并且有一个简单但值得解释的问题。

我发现它是int32的别名,其目的是区分数字和字符值。

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

但是我对术语"rune"感到困惑,它实际上代表什么?例如,uint == 无符号整数。

英文:

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

答案1

得分: 11

Rune代表字母。 (“符文”是一组相关字母表中的字母,被称为符文字母表,在采用拉丁字母表之前用于书写各种日耳曼语言。[维基百科])。

如果在Go中的变量具有类型rune,则表示它是用于保存Unicode码点的。 (runecodepoint更短更清晰)。但从技术上讲,它是int32,即在内存中的表示形式是int32。

英文:

> 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.

答案2

得分: 6

在一般意义上,Unicode的“rune”只是一个数字,就像64(0x40)是ASCII和Unicode中代表'@'的代码数字一样。

  • 64是一个实数吗?是的,当然。你可以将字面值64赋给一个浮点变量。
  • 64是一个整数吗?是的。你可以将字面值64赋给任何整数变量。
  • 64是一个有符号数吗?是的。你可以将字面值64赋给任何无符号变量。
  • 64是一个无符号数吗?是的。你可以将字面值64赋给任何有符号变量。

这段代码试图展示的是,[小]整数(以及这样的字面值)基本上是无类型的,即无类型

相关:Rune Literals

英文:

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.

huangapple
  • 本文由 发表于 2013年7月25日 18:41:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/17855774.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定