We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 49a1278 commit 73e425fCopy full SHA for 73e425f
examples/with_uint/main.go
@@ -0,0 +1,25 @@
1
+package main
2
+
3
+import (
4
+ "fmt"
5
+ "github.com/compression-algorithm-research-lab/go-varint"
6
+ "unsafe"
7
+)
8
9
+func main() {
10
11
+ // 现在有一个无符号数字
12
+ v := uint64(10)
13
+ // 对齐进行varint编码,可以得到一个字节切片
14
+ valueVarintBytes := varint.Encode[uint64](v)
15
+ // 原始的变量的值占用8个字节的内存,在存储的时候一般也是会视数据类型占用不同的字节数,uint64存储的时候也是要占用8个字节
16
+ fmt.Println(unsafe.Sizeof(v)) // Output: 8
17
+ // 编码之后存储的时候只需要占用一个字节即可,节省了7个字节的存储消耗
18
+ fmt.Println(len(valueVarintBytes)) // Output: 1
19
+ // 注意,varint仅仅是在存储的时候节省空间,并不适合在内存中以varint的方式表示,因为Golang表示数组会有额外的元数据开销(数组的头指针之类的)
20
+ fmt.Println(unsafe.Sizeof(valueVarintBytes)) // Output: 24
21
22
+ // 需要还原值的时候就解码
23
+ decodeValue := varint.Decode[uint64](valueVarintBytes)
24
+ fmt.Println(decodeValue) // Output: 10
25
+}
0 commit comments