Golang PutUvarint Uvarint

Posted 衣舞晨风

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Golang PutUvarint Uvarint相关的知识,希望对你有一定的参考价值。

bigcache库的时候,注意到存放数据用的是PutUvarint、Uvarint,那这两个方法是做什么的呢?

PutUvarint

源码及注释

// PutUvarint encodes a uint64 into buf and returns the number of bytes written.
// If the buffer is too small, PutUvarint will panic.
// PutVarint 将 int64 编码为 buf 并返回写入的字节数。如果缓冲区太小,PutVarint 会panic。
func PutUvarint(buf []byte, x uint64) int 
	i := 0
	// 0x80 128
	for x >= 0x80 
		buf[i] = byte(x) | 0x80
		x >>= 7
		i++
	
	buf[i] = byte(x)
	return i + 1

代码示例

package main

import (
	"encoding/binary"
	"fmt"
)

func main() 
	buf := make([]byte, binary.MaxVarintLen64)

	for _, x := range []int64-65, -64, -2, -1, 0, 1, 2, 63, 64 
		n := binary.PutVarint(buf, x)
		fmt.Printf("%x\\n", buf[:n])
	

输出结果

8101
7f
03
01
00
02
04
7e
8001

Program exited.

https://go.dev/play/p/yRUoUooVrHm

Uvarint

源码及注释

// Uvarint decodes a uint64 from buf and returns that value and the
// number of bytes read (> 0). If an error occurred, the value is 0
// and the number of bytes n is <= 0 meaning:
//
//	n == 0: buf too small
//	n  < 0: value larger than 64 bits (overflow)
//	        and -n is the number of bytes read
// Uvarint 从 buf 解码 uint64 并返回该值和读取的字节数(> 0)。如果发生错误,则该值为0,并且字节数n <= 0意味着:
// n == 0:buf太小了
// n <0:大于64位的值(溢出)
//       和-n是读取的字节数
func Uvarint(buf []byte) (uint64, int) 
	var x uint64
	var s uint
	for i, b := range buf 
		// MaxVarintLen64 10
		if i == MaxVarintLen64 
			// Catch byte reads past MaxVarintLen64.
			// See issue https://golang.org/issues/41185
			return 0, -(i + 1) // overflow
		
		// 0x80 128
		if b < 0x80 
			if i == MaxVarintLen64-1 && b > 1 
				return 0, -(i + 1) // overflow
			
			return x | uint64(b)<<s, i + 1
		
		// 0x7f 127
		x |= uint64(b&0x7f) << s
		s += 7
	
	return 0, 0

代码示例

package main

import (
	"encoding/binary"
	"fmt"
)

func main() 
	inputs := [][]byte
		[]byte0x01,       //1
		[]byte0x02,       //2
		[]byte0x7f,       //127
		[]byte0x80, 0x01, //128,1
		[]byte0xff, 0x01, //255,1
		[]byte0x80, 0x02, //128,2
		[]byte0x80, 0x03, //128,3
	
	for _, b := range inputs 
		x, n := binary.Uvarint(b)
		if n != len(b) 
			fmt.Println("Uvarint did not consume all of in")
		
		fmt.Println(x)
	


输出结果

1
2
127
128
255
256
384

Program exited.

https://go.dev/play/p/df572cFK6Im

在线16进制转10进制:https://jisuan5.com/hexadecimal-to-decimal/
参考文章:https://cloud.tencent.com/developer/section/1141534

以上是关于Golang PutUvarint Uvarint的主要内容,如果未能解决你的问题,请参考以下文章

Golang PutUvarint Uvarint

Golang 学习之路

Golang 入门

Golang入门到项目实战 第一个golang应用

golang编译androidso无法加载

golang如何打印内存内容