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
+23.2k Golang : Print out struct values in string format
+6.5k Grep : How to grep for strings inside binary data
+9.8k Golang : Qt get screen resolution and display on center example
+17.9k Golang : Login and logout a user after password verification and redirect example
+25.9k Golang : How to read integer value from standard input ?
+23.1k Golang : Randomly pick an item from a slice/array example
+10k CodeIgniter : Load different view for mobile devices
+22.5k Golang : Convert Unix timestamp to UTC timestamp
+6.9k Golang : Normalize email to prevent multiple signups example
+14.8k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+7.5k Golang : Handling Yes No Quit query input
+14.1k Golang : Check if a file exist or not