从[] byte读取uint8而不创建byte.Buffer

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??

Us an index expression to get an single uint8 from a slice.

len := binary.BigEndian.Uint32(b[:4])
id := b[4]  // <-- index expression

Note that byte is an alias for uint8.

Here's an elegant way that I found,

var len uint32
var id uint8
binary.Read(buf, binary.BigEndian, &len)
binary.Read(buf, binary.BigEndian, &id)

// this method doesn't need you to create a buffer for each value read