Golang encoding/binary.ReadVarint() function example
package encoding/binary
ReadVarint reads an encoded signed integer from r (1st parameter) and returns it as an int64.
Golang encoding/binary.ReadVarint() function usage example
package main
import (
"bytes"
"encoding/binary"
"fmt"
)
func main() {
var emptybuf []byte
var buf []byte = []byte{144, 192, 192, 129, 132, 136, 140, 144, 16, 0, 1, 1}
var overflowbuf []byte = []byte{144, 192, 192, 129, 132, 136, 140, 144, 192, 192, 1, 1}
num, err := binary.ReadVarint(bytes.NewBuffer(emptybuf))
fmt.Println(num, err)
num, err = binary.ReadVarint(bytes.NewBuffer(buf))
fmt.Println(num, err)
num, err = binary.ReadVarint(bytes.NewBuffer(overflow))
fmt.Println(num, err)
}
Output :
0 EOF
580990878187261960
2310373135097532424 binary: varint overflows a 64-bit integer
Reference :
See also : Golang encoding/binary.ReadUvarint() function example
Advertisement
Something interesting
Tutorials
+6.5k Apt-get to install and uninstall Golang
+11.9k Golang : Calculations using complex numbers example
+7.7k Golang : Get YouTube playlist
+8.1k Golang : Example of how to detect which type of script a word belongs to
+40.5k Golang : UDP client server read write example
+21.7k Golang : How to force compile or remove object files first before rebuild?
+7.1k Mac/Linux/Windows : Get CPU information from command line
+9.3k Golang : How to use Gorilla webtoolkit context package properly
+22.1k SSL : How to check if current certificate is sha1 or sha2
+12.4k Golang : md5 hash of a string
+14.6k Golang : Chunk split or divide a string into smaller chunk example
+10.1k Golang : Resumable upload to Google Drive(RESTful) example