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
+14.2k Elastic Search : Mapping date format and sort by date
+14.6k Golang : Execute function at intervals or after some delay
+8.6k Golang : Set or add headers for many or different handlers
+6.5k Grep : How to grep for strings inside binary data
+18k Golang : How to log each HTTP request to your web server?
+15k Golang : Search folders for file recursively with wildcard support
+7.8k Golang : Lock executable to a specific machine with unique hash of the machine
+5.2k Python : Create Whois client or function example
+17.6k Convert JSON to CSV in Golang
+8.6k Golang : Another camera capture GUI application with GTK and OpenCV
+9.4k Facebook : Getting the friends list with PHP return JSON format
+22k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."