字节是内置类型,难道不应该是uint8吗?

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

byte builtin type, isn’t it supposed to be uint8?

问题

我在byte godoc中发现type byte byte非常令人困惑,难道不应该是type byte uint8吗?

byte是uint8的别名,在所有方面都等同于uint8。按照惯例,它用于区分字节值和8位无符号整数值。type complex128

英文:

I find type byte byte very confusing in byte godoc , isn’t supposed to be type byte uint8 ?

> byte is an alias for uint8 and is equivalent to uint8 in all ways. It
> is used, by convention, to distinguish byte values from 8-bit unsigned
> integer values. type complex128

答案1

得分: 5

没有真正的package builtin。但是为了解释内置函数,为godoc生成了一个带有合成类型的合成包builtin。类型byte从未真正声明为type byte byte,只是因为byte是内置的,不需要声明。(而且byte不是也不应该声明为type byte uint8:byte是内置的,不需要声明。)

只需忽略合成声明,阅读描述即可。描述才是有用的内容。

英文:

There is no real package builtin. But to explain the builtins a synthetic package builtin with synthetic types is generated for godoc. The type byte is never realy declared as type byte byte simply because byte is builtin and doesn't need a declaration. (And no byte is not and should not be declared as type byte uint8: byte is bultin and not declared.)

Just ignore the synthetic declarations and read the description. The description is the useful stuff here.

答案2

得分: 2

我认为答案在包的介绍中:

> 这里记录的项目实际上并不在内置包中,但是它们在这里的描述允许 godoc 展示语言特殊标识符的文档。

这个语法没有实际意义,只是一种占位符,用于像其他(非内置)类型一样记录类型 byte 的文档。byte 在内部定义,而且 type byte byte 这一行实际上永远不会遇到。作为内部类型,它不会被定义为 uint8 的“常规”别名,后者应该是 type newbyte uint8(尽管 type newbyte byte 也是有效的,可能更清晰)。

英文:

I think the answer is in the presentation of the package:

> The items documented here are not actually in package builtin but their descriptions here allow godoc to present documentation for the language's special identifiers.

This syntax has no real meaning, it is just a sort of placeholder for having the type byte being documented like other (non built-in) types. byte is defined internally, and the line type byte byte is never actually encountered. Being internal, it would not be defined as a 'regular' alias of uint8, which would indeed be type newbyte uint8 (although type newbyte byte would be as valid and probably clearer).

huangapple
  • 本文由 发表于 2013年11月22日 19:52:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/20144297.html
匿名

发表评论

匿名网友

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

确定