Skip to content

Commit

Permalink
Update Varint.md
Browse files Browse the repository at this point in the history
  • Loading branch information
wjvlangz committed Mar 27, 2013
1 parent c353558 commit faf48be
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions encoding/binary/Varint.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,41 @@

###参数列表

- buf 需写入的缓冲区
- x uint64类型数字
- buf 需要解码的缓冲区

###返回值:

- int 写入字节数
- panic buf过小
- int64 解码的数据
- int 解析的字节数

###功能说明:

PutUvarint主要是将uint64类型放入buf中,并返回写入的字节数。如果buf过小,putUvarint将抛出panic。
Varint是从buf中解码并返回一个int64的数据,及解码的字节数(>0)。
如果出错,则返回数据0和一个小于等于0的字节数n,其意义为:

n == 0:buf太小
n < 0:数据太大,超过64位,且-n为已解析字节数

###代码实例:

package main

import (
"encoding/binary"
"fmt"
)

func main() {
var sbuf []byte
var buf []byte = []byte{144, 192, 192, 129, 132, 136, 140, 144, 16, 0, 1, 1}
var bbuf []byte = []byte{144, 192, 192, 129, 132, 136, 140, 144, 192, 192, 1, 1}

num, ret := binary.Varint(sbuf)
fmt.Println(num, ret) //0 0

num, ret = binary.Varint(buf)
fmt.Println(num, ret) //580990878187261960 9

num, ret = binary.Varint(bbuf)
fmt.Println(num, ret) //0 -11
}

0 comments on commit faf48be

Please sign in to comment.