Golang encoding/binary.ReadUvarint() function example
package encoding/binary
ReadUvarint reads an encoded unsigned integer from r(1st parameter) and returns it as a uint64.
Golang encoding/binary.ReadUvarint() 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.ReadUvarint(bytes.NewBuffer(emptybuf))
fmt.Println(num, err)
num, err = binary.ReadUvarint(bytes.NewBuffer(buf))
fmt.Println(num, err)
num, err = binary.ReadUvarint(bytes.NewBuffer(overflowbuf))
fmt.Println(num, err)
}
Output :
0 EOF
1161981756374523920
4620746270195064848 binary: varint overflows a 64-bit integer
Reference :
See also : Golang encoding/binary.ReadVarint() function example
Advertisement
Something interesting
Tutorials
+8.4k Golang : How to check if input string is a word?
+10k Golang : Channels and buffered channels examples
+5.4k Golang : Get S3 or CloudFront object or file information
+5.8k Golang : List all packages and search for certain package
+22.4k Golang : How to read JPG(JPEG), GIF and PNG files ?
+19.6k Golang : Close channel after ticker stopped example
+5.6k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+5.5k Golang : Stop goroutine without channel
+13.4k Golang : Get constant name from value
+4.7k Linux/MacOSX : How to symlink a file?
+20.3k Golang : Check if os.Stdin input data is piped or from terminal
+9.3k Golang : How to protect your source code from client, hosting company or hacker?