go – uint64是否需要8个字节的存储空间?

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

编辑:

谢谢大家的回答!

当我注意到binary.PutUvarint消耗多达10个字节来存储大的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).
根据 http://golang.org/ref/spec#Size_and_alignment_guarantees
type                                 size in bytes

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

所以,是的,uint64总是占用8个字节.

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...