加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

为什么符文符号在golang是一个别名为int32而不是uint32?

发布时间:2020-12-16 19:29:24 所属栏目:大数据 来源:网络整理
导读:Go中的类型符号是 defined作为“int32的别名,并且在所有方面相当于int32,它被按照惯例用于区分字符值和整数值. 如果使用这种类型来表示字符值,为什么Go语言的作者不使用uint32而不是int32?当他们是否定的时候,他们如何期待在程序中处理符文值?其他类似的字
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中) .

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读