github.com/webmafia/fast@v0.10.0/binary/stream_write_uint.go (about) 1 // Borrowed from jsoniter (https://github.com/json-iterator/go) 2 package binary 3 4 import ( 5 "encoding/binary" 6 ) 7 8 // Write uint8 9 func (b *StreamWriter) WriteUint8(v uint8) error { 10 return b.WriteByte(v) 11 } 12 13 // Write uint16 14 func (b *StreamWriter) WriteUint16(v uint16) (err error) { 15 _, err = b.Write([]byte{ 16 byte(v), 17 byte(v >> 8), 18 }) 19 return 20 } 21 22 // Write uint32 23 func (b *StreamWriter) WriteUint32(v uint32) (err error) { 24 _, err = b.Write([]byte{ 25 byte(v), 26 byte(v >> 8), 27 byte(v >> 16), 28 byte(v >> 24), 29 }) 30 return 31 } 32 33 // Write uint64 34 func (b *StreamWriter) WriteUint64(v uint64) (err error) { 35 _, err = b.Write([]byte{ 36 byte(v), 37 byte(v >> 8), 38 byte(v >> 16), 39 byte(v >> 24), 40 byte(v >> 32), 41 byte(v >> 40), 42 byte(v >> 48), 43 byte(v >> 56), 44 }) 45 return 46 } 47 48 // Write uint 49 func (b *StreamWriter) WriteUint(v uint) error { 50 return b.WriteUint64(uint64(v)) 51 } 52 53 func (b *StreamWriter) WriteUvarint(v uint64) (err error) { 54 var buf [binary.MaxVarintLen64]byte 55 n := binary.PutUvarint(buf[:], v) 56 _, err = b.Write(buf[:n]) 57 return 58 }