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) intParameters & Attributes
| Name | Type | Description |
|---|---|---|
| buf | []byte | Byte buffer used for the operation; append APIs extend it. |
| x | int64 | Integer 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])}