code: func (sp *Subscribe) Encode(dst []byte) (int, error) {
  	total := 0
  
  	// check packet id
  	if !sp.ID.Valid() {
  		return total, makeError(sp.Type(), "packet id must be grater than zero")
  	}
  
  	// encode header
  	n, err := headerEncode(dst[total:], 0, sp.len(), sp.Len(), SUBSCRIBE)
  	total += n
  	if err != nil {
  		return total, err
  	}
  
  	// write packet id
  	binary.BigEndian.PutUint16(dst[total:], uint16(sp.ID))
  	total += 2
  
  	for _, t := range sp.Subscriptions {
  		// write topic
  		n, err := writeLPString(dst[total:], t.Topic, sp.Type())
  		total += n
  		if err != nil {
  			return total, err
  		}
  
  		// check qos
  		if !t.QOS.Successful() {
  			return total, makeError(sp.Type(), "invalid QOS level (%d)", t.QOS)
  		}
  
  		// write qos
  		dst[total] = byte(t.QOS)
  
  		total++
  	}
  
  	return total, nil
  }
docs: // Encode writes the packet bytes into the byte slice from the argument. It
  // returns the number of bytes encoded and whether there's any errors along
  // the way. If there is an error, the byte slice should be considered invalid.