The Len wire type prefixes its value with a varint byte count, and that one mechanism carries strings, bytes, embedded messages, and packed fields alike. Today you read a length-delimited chunk: a varint length, then exactly that many raw bytes.
Read a length-delimited value as a varint length followed by that many bytes.
The Len wire type is the workhorse of protobuf. Its value is written as a varint length followed by exactly that many bytes of payload, and the decoder does not need to know what those bytes mean to read past them - a string, a nested message, and a packed list of integers all look identical at this level: a count and a blob. That uniformity is why one wire type covers so much.
Read the length as a varint, then take that many bytes. Two edges matter: a length
of 0 is legal and yields an empty payload (an empty string is a real value), and a
length that claims more bytes than remain in the buffer is malformed and must
error rather than slice out of bounds. Guard pos + n against the buffer length
before slicing. Later lessons will hand these payload bytes to a fresh reader to
decode strings and recurse into nested messages.
// read the varint length, then slice off that many bytesfunc (r *Reader) ReadBytes() ([]byte, error) {n, err := r.ReadVarint()if err != nil { return nil, err }// bounds-check n against what remains before slicingout := r.buf[r.pos : r.pos+int(n)]r.pos += int(n)return out, nil}