Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go uint64是否需要8字节的存储空间?_Go - Fatal编程技术网

Go uint64是否需要8字节的存储空间?

Go uint64是否需要8字节的存储空间?,go,Go,官方文件说uint64是一个64位的无符号整数,这是否意味着任何uint64数字都应该占用8字节的存储空间,不管它有多小或多大 编辑: 谢谢大家的回答 当我注意到binary.PutUvarint存储大型uint64时,我提出了疑问,尽管最大uint64应该只需要8个字节 然后我在Golang lib的源代码中找到了我的疑问的答案: Design note: // At most 10 bytes are needed for 64-bit values. The encoding could

官方文件说uint64是一个64位的无符号整数,这是否意味着任何uint64数字都应该占用8字节的存储空间,不管它有多小或多大

编辑:

谢谢大家的回答

当我注意到
binary.PutUvarint
存储大型
uint64
时,我提出了疑问,尽管最大
uint64
应该只需要8个字节

然后我在Golang lib的源代码中找到了我的疑问的答案:

Design note:
// At most 10 bytes are needed for 64-bit values. The encoding could
// be more dense: a full 64-bit value needs an extra byte just to hold bit 63.
// Instead, the msb of the previous byte could be used to hold bit 63 since we
// know there can't be more than 64 bits. This is a trivial improvement and
// would reduce the maximum encoding length to 9 bytes. However, it breaks the
// invariant that the msb is always the "continuation bit" and thus makes the
// format incompatible with a varint encoding for larger numbers (say 128-bit).

简单地说:是的,64位固定大小的整数类型总是需要8个字节。如果不是这样的话,这将是一种不同寻常的语言

有些语言/平台支持可变长度的数字类型,其中内存中的存储确实取决于值,但您不会以如此简单的方式指定类型中的位数,因为这可能会有所不同

Go编程语言规范

数字类型表示整数值或浮点值集。 预先声明的与体系结构无关的数字类型有:

uint64      the set of all unsigned 64-bit integers (0 to 18446744073709551615)
是的,正好是64位或8字节。

根据:


因此,是的,uint64总是需要8个字节。

请记住一条简单的规则,变量类型通常经过优化以适合特定的内存空间,最小内存空间为1位。和8位=1字节:

因此64位=8字节

type                                 size in bytes

byte, uint8, int8                     1
uint16, int16                         2
uint32, int32, float32                4
uint64, int64, float64, complex64     8
complex128                           16