Standard library / encoding/binary

encoding/binary.PutUvarint()

Writes an unsigned varint into existing storage; provide enough capacity to avoid panic. Expected stdout: 'ac02\n'.

Formal Syntax & Structure

go syntaxgo
func PutUvarint(buf []byte, x uint64) int

Parameters & Attributes

NameTypeDescription
buf[]byteByte buffer used for the operation; append APIs extend it.
xuint64Integer value to encode, or length for DecodedLen.

Return Value

int

Number of bytes or required size, as described above.

Working Example

Example Snippetgo
package main
import (  "encoding/binary"  "fmt")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  p := make([]byte, binary.MaxVarintLen64)  n := binary.PutUvarint(p, 300)  fmt.Printf("%x\n", p[:n])}
Go Reference