package main import ( "fmt" "github.com/gogo/protobuf/proto" ) func main() { buf := make([]byte, 0, 10) buf = proto.EncodeVarint(42, buf) fmt.Println(buf) }
package main import ( "bytes" "encoding/binary" "fmt" "github.com/gogo/protobuf/proto" ) func main() { varint := uint32(123456789) buf := new(bytes.Buffer) binary.Write(buf, binary.LittleEndian, varint) bytes := buf.Bytes() fmt.Println("Original bytes:", bytes) encodedVarint := proto.EncodeVarint(uint64(varint), nil) fmt.Println("Encoded Varint:", encodedVarint) }This example demonstrates how to encode an unsigned 32-bit integer (`varint`) into a varint-encoded byte slice using the `EncodeVarint` function. It first encodes the original integer into a byte slice using the `binary.Write` function from the `encoding/binary` package. Then, it constructs a new byte slice from the `bytes.Buffer` object and prints the original bytes to the console. Finally, it calls the `EncodeVarint` function to encode the integer as a varint and prints the result to the console.