Standard library / encoding/binary

encoding/binary.PutVarint()

Writes a signed varint into existing storage. The returned count is the encoded length. Expected stdout: '03\n'.

Formal Syntax & Structure

go syntaxgo
func PutVarint(buf []byte, x int64) int

Parameters & Attributes

NameTypeDescription
buf[]byteByte buffer used for the operation; append APIs extend it.
xint64Integer 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.PutVarint(p, -2)  fmt.Printf("%x\n", p[:n])}
Go Reference