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
+8k Golang : Sort words with first uppercase letter
+7.6k Javascript : Push notifications to browser with Push.js
+9.1k Golang : Get curl -I or head data from URL example
+12.4k Golang : Search and extract certain XML data example
+23.5k Golang : Check if element exist in map
+15.9k Golang : Update database with GORM example
+34.1k Golang : Create x509 certificate, private and public keys
+14.2k Golang : Fix image: unknown format error
+12.3k Golang : Print UTF-8 fonts on image example
+5.8k Unix/Linux : How to test user agents blocked successfully ?
+12k Golang : Find and draw contours with OpenCV example
+31.5k Golang : bufio.NewReader.ReadLine to read file line by line