Go中的类型符号是
defined作为“int32的别名,并且在所有方面相当于int32,它被按照惯例用于区分字符值和整数值.
如果使用这种类型来表示字符值,为什么Go语言的作者不使用uint32而不是int32?当他们是否定的时候,他们如何期待在程序中处理符文值?其他类似的字节是uint(而不是int)的别名,这是合理的.
“
Golang,Go : what is rune by the way?”提到:
With the recent Unicode 6.3,there are over 110,000 symbols defined. This requires at least 21-bit representation of each code point,so a rune is like int32 and has plenty of bits.
但是关于溢出或负值问题,请注意,执行某些unicode函数(如unicode.IsGraphic)包括:
We convert to
uint32
to avoid the extra test for negative
码:
const MaxLatin1 = '\u00FF' // maximum Latin-1 value. // IsGraphic reports whether the rune is defined as a Graphic by Unicode. // Such characters include letters,marks,numbers,punctuation,symbols,and // spaces,from categories L,M,N,P,S,Zs. func IsGraphic(r rune) bool { // We convert to uint32 to avoid the extra test for negative,// and in the index we convert to uint8 to avoid the range check. if uint32(r) <= MaxLatin1 { return properties[uint8(r)]&pg != 0 } return In(r,GraphicRanges...) }
这可能是因为符文应该是constant(如“Go rune type explanation”所述),其中符文可以在一个int32或uint32甚至是float32或…中:它的常数值授权它存储在任何numeric types中) .