Skip to content

Instantly share code, notes, and snippets.

@quux00
Last active August 29, 2015 14:24
Show Gist options
  • Save quux00/6c466c07b2c334d91352 to your computer and use it in GitHub Desktop.
Save quux00/6c466c07b2c334d91352 to your computer and use it in GitHub Desktop.
Revised VarintEncode function
func VarintEncode(w io.Writer, v uint64) error {
bs := make([]byte, 0, 10)
for (v & 0xffffffffffffff80) != 0 {
bs = append(bs, byte((v&0x7f)|0x80))
v >>= 7
}
bs = append(bs, byte(v&0x7f))
n, err := w.Write(bs)
if err != nil {
return oerror.NewTrace(err)
}
if n != len(bs) {
return fmt.Errorf("Incorrect number of bytes written. "+
"Expected %d. Actual %d", len(bs), n)
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment