英文:
Why does type int exist in Go
问题
在Golang中有int,int32,int64
这几种类型。
int32
有32位,int64
有64位,int
根据环境可以有32位、64位或其他位数。
我认为对于程序来说,int32
和int64
完全足够了。我不知道为什么还需要int
类型,它不会让我们的代码行为更难以预测吗?
而且在C++中,类型int
和long
的长度是不确定的。我觉得这会让我们的程序变得脆弱。我感到很困惑。
英文:
There's int, int32, int64
in Golang
.
int32 has 32 bits,
int64 has 64 bits,
int has 32 or 64 or different number of bits according to the environment.
I think int32
and int64
will be totally enough for the program.
I don't know why int
type should exist, doesn't it will make the action of our code harder to predict?
And also in C++
, type int
and type long
have uncertain length. I think it will make our program fragile. I'm quite confused.
答案1
得分: 6
通常,int
的大小等于目标平台的自然字长。因此,如果你的程序不关心 int
的大小(只要 int
的范围足够小),它可以在各种编译器上表现最佳。
当你需要特定的大小时,你可以使用 int32
等。
英文:
Usually size of int
is equal to the natural word size of target. So if your program doesn't care for the size of int
(Minimal int
range is enough), it can perform best on variety of compilers.
When you need a specific size, you can of course use int32
etc.
答案2
得分: 6
通常,每个平台都最适合使用其本机大小的整数类型。
通过使用简单的int
,你告诉编译器你并不真的关心使用哪种位宽,而是让它选择一个能够以最快速度工作的位宽。请注意,你总是希望编写的代码尽可能与平台无关...
另一方面,如果你需要整数具有特定的大小,那么int32
/ int64
类型就很有用。这在保存二进制文件时可能很有用(不要忘记字节序)。或者如果你有一个大型整数数组(其值最多只能达到32位),那么节省一半的内存将是很重要的,等等。
英文:
Usually each platform operates best with integral type of its native size.
By using simple int
you say to your compiler that you don't really care about what bit width to use and you let him choose the one it will work fastest with. Note, that you always want to write your code so that it is as platform independent as possible...
On the other hand int32
/ int64
types are useful if you need the integer to be of a specific size. This might be useful f.e. if you want to save binary files (don't forget about endiannes). Or if you have large array of integers (that will only reach up to 32b value), where saving half the memory would be significant, etc.
答案3
得分: 3
在Go的1.0版本中,int
只是int32
的同义词,即32位整数。由于slices
使用int
作为索引,这导致slices
的元素数量不能超过大约20亿个。
在Go 1.1中,int
在64位平台上被扩展为64位长,因此足够大以索引任何适合主内存的slices
。因此:
int32
是32位整数的类型;int64
是64位整数的类型;int
是能够索引所有可能的slices
的最小整数类型。
实际上,对于大多数实际用途来说,int
已经足够大。只有在处理大于最大可能的slices
索引的值时,才需要使用int64
,而当不需要较大范围时,使用int32
可以节省内存并减少内存流量。
英文:
In versions of Go up to 1.0, int
was just a synonym for int32
— a 32-bit Integer. Since int
is used for indexing slices, this prevented slices from having more than 2 billion elements or so.
In Go 1.1, int
was made 64 bits long on 64-bit platforms, and therefore large enough to index any slice that fits in main memory. Therefore:
int32
is the type of 32-bit integers;int64
is the type of 64-bit integers;int
is the smallest integer type that can index all possible slices.
In practice, int
is large enough for most practical uses. Using int64
is only necessary when manipulating values that are larger than the largest possible slice index, while int32
is useful in order to save memory and reduce memory traffic when the larger range is not necessary.
答案4
得分: 1
这个问题的根本原因是数组的可寻址性。如果你遇到需要调用 make([]byte, 5e9)
的情况,32位可执行文件将无法满足要求,而64位可执行文件可以继续运行。在32位构建中使用 int64
寻址数组是浪费的。在64位构建中使用 int32
寻址数组是不够的。使用 int
,你可以在这两种架构上将数组寻址到其最大分配大小,而无需使用 int32
/int64
进行区分编码。
英文:
The root cause for this is array addressability. If you came into a situation where you needed to call make([]byte, 5e9)
your 32 bit executable would be unable to comply, while your 64 bit executable could continue to run. Addressing an array with int64
on a 32 bit build is wasteful. Addressing an array with int32
on a 64 bit build is insufficient. Using int
you can address an array to its maximum allocation size on both architectures without having to code a distinction using int32
/int64
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论