github.com/gogf/gf@v1.16.9/.example/encoding/gbinary/bits2.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/gogf/gf/encoding/gbinary"
     7  )
     8  
     9  func main() {
    10  	// Meta元数据文件数据结构:[键名哈希64(64bit,8byte) 键名长度(8bit,1byte) 键值长度(24bit,3byte) 数据文件偏移量(40bit,5byte)](变长)
    11  	hash := 521369841259754125
    12  	klen := 12
    13  	vlen := 35535
    14  	offset := 80000000
    15  
    16  	// 编码
    17  	bits := make([]gbinary.Bit, 0)
    18  	bits = gbinary.EncodeBits(bits, hash, 64)
    19  	bits = gbinary.EncodeBits(bits, klen, 8)
    20  	bits = gbinary.EncodeBits(bits, vlen, 24)
    21  	bits = gbinary.EncodeBits(bits, offset, 40)
    22  	buffer := gbinary.EncodeBitsToBytes(bits)
    23  	fmt.Println("meta length:", len(buffer))
    24  
    25  	/* 文件存储及数据查询过程忽略,这里只展示元数据编码/解码示例 */
    26  
    27  	// 解码
    28  	metabits := gbinary.DecodeBytesToBits(buffer)
    29  	fmt.Println("hash  :", gbinary.DecodeBits(metabits[0:64]))
    30  	fmt.Println("klen  :", gbinary.DecodeBits(metabits[64:72]))
    31  	fmt.Println("vlen  :", gbinary.DecodeBits(metabits[72:96]))
    32  	fmt.Println("offset:", gbinary.DecodeBits(metabits[96:136]))
    33  }