Why does type int exist in Go

huangapple go评论79阅读模式
英文:

Why does type int exist in Go

问题

在Golang中有int,int32,int64这几种类型。

  • int32有32位,
  • int64有64位,
  • int根据环境可以有32位、64位或其他位数。

我认为对于程序来说,int32int64完全足够了。我不知道为什么还需要int类型,它不会让我们的代码行为更难以预测吗?

而且在C++中,类型intlong的长度是不确定的。我觉得这会让我们的程序变得脆弱。我感到很困惑。

英文:

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.

huangapple
  • 本文由 发表于 2017年2月17日 15:11:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/42291567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定