The type rune
in Go is defined as
an alias for
int32
and is equivalent toint32
in all ways. It is used, by conv
"Golang, Go : what is rune by the way?" mentioned:
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.
But regarding the overflow or negative value issues, note that the implementation of some of the unicode functions like unicode.IsGraphic do include:
We convert to
uint32
to avoid the extra test for negative
Code:
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...)
}
That maybe because a rune is supposed to be constant (as mentioned in "Go rune type explanation", where a rune could be in an int32
or uint32
or even float32
or ...: its constant value authorizes it to be stored in any of those numeric types).