Golang中的int类型和uint类型到底有多大?
上面是图,下面是源码:今天调试一个问题,发现一个我无法理解的情况:把64位的数字赋值给uint,我理解uint是32位的,为啥可以编译通过?因为别的语言是这样,惯性思维了。int is a signed integer type that is at least 32 bits in size. It is a distinct type, however, and not an alias for, say, int32.uint和int情况差不多。翻译一下,就是说这个整形最少占32位,int和int32是两码事。uint is a variable sized type, on your 64 bit computer uint is 64 bits wide.我的理解uint类型长度取决于 CPU,如果是32位CPU就是4个字节,如果是64位就是8个字节。我的电脑是64位的,而 playground 是32位的,问题就出在这里。这里就会出现一个情况,int和uint是根据 CPU 变化的,如何知道当前系统的情况?写了这么多年 Golang,int天天用,一直被我当32位处理,说来惭愧。。。
下载地址
用户评论