How to read a unit8
from []byte
without creating a bytes.Buffer
. The value has been written to the buffer like this,
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, uint32(1))
binary.Write(buf, binary.BigEndian, uint8(1))
b := buf.Bytes()
While decoding, it can easily be done for uint32
, like the following...
len := binary.BigEndian.Uint32(b[:4])
But for the uint8
, the only way to retrieve the value that I could come up with, is to create a buffer and then read the first byte,
buf := new(bytes.Buffer)
_, err := buf.Write(b[4:5])
// error handling ...
id = buf.ReadByte()
It seems like there's no method in the encoding/binary
pkg for uint8
value retrieval. And I guess there's probably some good reason behind it.
Question: Is there any other way to read uint8
from that []byte
without creating a Buffer
??