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
+7.4k Golang : How to detect if a sentence ends with a punctuation?
+14k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+22.2k Golang : Convert seconds to minutes and remainder seconds
+8.2k Golang : Routes multiplexer routing example with regular expression control
+20.2k Golang : How to get struct tag and use field name to retrieve data?
+8.8k Golang : Heap sort example
+20.4k nginx: [emerg] unknown directive "passenger_enabled"
+8.9k Golang : Gaussian blur on image and camera video feed examples
+32.2k Golang : Convert []string to []byte examples
+6.4k Golang : How to search a list of records or data structures
+13.4k Golang : Increment string example