官方文档说 uint64 是一个 64 位的无符号整数,这是否意味着任何 uint64 数字都应该占用 8 个字节的存储空间,无论它有多小或多大?
编辑:
谢谢大家的回答!
当我注意到binary.PutUvarint最多消耗 10 个字节来存储一个 large 时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).
繁星coding
ITMISS
相关分类