Golang encoding/binary.Varint() function examples
package encoding/binary
Varint decodes an int64 from buf (1st parameter) and returns that value and the number of bytes read (> 0). If an error occurred, the value is 0 and the number of bytes n is <= 0 with the following meaning:
n == 0: buf too small
n < 0: value larger than 64 bits (overflow) and -n is the number of bytes read
Golang encoding/binary.Varint() function usage examples
Example 1 :
package main
import (
"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, ret := binary.Varint(emptybuf)
fmt.Println(num, ret)
num, ret = binary.Varint(buf)
fmt.Println(num, ret)
num, ret = binary.Varint(overflowbuf)
fmt.Println(num, ret)
}
Output :
0 0
580990878187261960 9
0 -11
Example 2 :
func randUInt64() uint64 {
buf, _ := randBytes(8)
res, _ := binary.Varint(buf)
return uint64(res)
}
References :
Advertisement
Something interesting
Tutorials
+7.4k Golang : Example of custom handler for Gorilla's Path usage.
+13.6k Golang : reCAPTCHA example
+16.3k Golang : convert string or integer to big.Int type
+9.4k Golang : Qt Yes No and Quit message box example
+10.5k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+9.9k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+26k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+21.8k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+8.3k Useful methods to access blocked websites
+12.4k Golang : Search and extract certain XML data example
+12.2k Golang : List running EC2 instances and descriptions
+14k Golang : concatenate(combine) strings