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.6k Golang : Convert(cast) int to float example
+20.3k Swift : Convert (cast) Int to int32 or Uint32
+28.5k Golang : Change a file last modified date and time
+12.1k Golang : Split strings into command line arguments
+9.1k Golang : Get curl -I or head data from URL example
+13.9k Golang : How to check if a file is hidden?
+12.1k Golang : convert(cast) string to integer value
+6.9k Golang : How to solve "too many .rsrc sections" error?
+18.2k Golang : Put UTF8 text on OpenCV video capture image frame
+6.2k Golang & Javascript : How to save cropped image to file on server
+10.2k Golang : Text file editor (accept input from screen and save to file)
+22.1k Golang : Repeat a character by multiple of x factor