header.go
func (h *Header) MarshalBinary() ([]byte, error) {
	// Version must be 1
	if h.Version != Version {
		return nil, ErrorUnsupportedVersion
	}

	// Arg must not be nil
	if h.Arg == nil {
		return nil, ErrorBadArgumentParameter
	}
	ab, err := h.Arg.MarshalBinary()
	if err != nil {
		return nil, err
	}

	// Allocate correct number of bytes for header and argument
	b := make([]byte, headerLen+len(ab))

	// Place Version in top 4 bits of first byte
	var vf uint8
	vf |= h.Version << 4

	// If needed, place response and error flags in bits 5 and 6 of first byte
	if h.FlagResponse {
		vf |= (1 << 3)
	}
	if h.FlagError {
		vf |= (1 << 2)
	}
	b[0] = vf

	// Store other fields directly in network byte order
	b[1] = uint8(h.Error)
	binary.BigEndian.PutUint16(b[2:4], h.Major)
	b[4] = h.Minor
	b[5] = uint8(h.Command)
	copy(b[6:10], h.Tag[:])

	// Copy argument data into end of header
	copy(b[10:], ab)

	return b, nil
}
func (h *Header) MarshalBinary() ([]byte, error) {
	// Version must be 1
	if h.Version != Version {
		return nil, ErrorUnsupportedVersion
	}

	// Arg must not be nil
	if h.Arg == nil {
		return nil, ErrorBadArgumentParameter
	}
	ab, err := h.Arg.MarshalBinary()
	if err != nil {
		return nil, err
	}

	// Allocate correct number of bytes for header and argument
	b := make([]byte, headerLen+len(ab))

	// Place Version in top 4 bits of first byte
	var vf uint8
	vf |= h.Version << 4

	// If needed, place response and error flags in bits 5 and 6 of first byte
	if h.FlagResponse {
		vf |= (1 << 3)
	}
	if h.FlagError {
		vf |= (1 << 2)
	}
	b[0] = vf

	// Store other fields directly in network byte order
	b[1] = uint8(h.Error)
	binary.BigEndian.PutUint16(b[2:4], h.Major)
	b[4] = h.Minor
	b[5] = uint8(h.Command)
	copy(b[6:10], h.Tag[:])

	// Copy argument data into end of header
	copy(b[10:], ab)

	return b, nil
}
// MarshalBinary allocates a byte slice containing the data from a Header.
//
// If h.Version is not Version (1), ErrorUnsupportedVersion is returned.
//
// If h.Arg is nil, ErrorBadArgumentParameter is returned.