configarg.go
func (c *ConfigArg) MarshalBinary() ([]byte, error) {
	// Command must be a 4-bit integer
	if c.Command > 0xf {
		return nil, ErrorBadArgumentParameter
	}

	// StringLength must indicate actual length of String
	if int(c.StringLength) != len(c.String) {
		return nil, ErrorBadArgumentParameter
	}

	// StringLength must not be greater than 1024, per AoEr11, Section 3.2.
	if c.StringLength > 1024 {
		return nil, ErrorBadArgumentParameter
	}

	// Allocate correct number of bytes for argument and config string
	b := make([]byte, configArgLen+int(c.StringLength))

	// Store basic information
	binary.BigEndian.PutUint16(b[0:2], c.BufferCount)
	binary.BigEndian.PutUint16(b[2:4], c.FirmwareVersion)
	b[4] = c.SectorCount

	// Set version in 4 most significant bits of byte 5; command in least
	// significant 4 bits
	var vc uint8
	vc |= c.Version << 4
	vc |= uint8(c.Command)
	b[5] = vc

	// Store config string length and string itself
	binary.BigEndian.PutUint16(b[6:8], c.StringLength)
	copy(b[8:], c.String)

	return b, nil
}
func (c *ConfigArg) MarshalBinary() ([]byte, error) {
	// Command must be a 4-bit integer
	if c.Command > 0xf {
		return nil, ErrorBadArgumentParameter
	}

	// StringLength must indicate actual length of String
	if int(c.StringLength) != len(c.String) {
		return nil, ErrorBadArgumentParameter
	}

	// StringLength must not be greater than 1024, per AoEr11, Section 3.2.
	if c.StringLength > 1024 {
		return nil, ErrorBadArgumentParameter
	}

	// Allocate correct number of bytes for argument and config string
	b := make([]byte, configArgLen+int(c.StringLength))

	// Store basic information
	binary.BigEndian.PutUint16(b[0:2], c.BufferCount)
	binary.BigEndian.PutUint16(b[2:4], c.FirmwareVersion)
	b[4] = c.SectorCount

	// Set version in 4 most significant bits of byte 5; command in least
	// significant 4 bits
	var vc uint8
	vc |= c.Version << 4
	vc |= uint8(c.Command)
	b[5] = vc

	// Store config string length and string itself
	binary.BigEndian.PutUint16(b[6:8], c.StringLength)
	copy(b[8:], c.String)

	return b, nil
}
// MarshalBinary allocates a byte slice containing the data from a ConfigArg.
//
// If any of the following conditions occur, ErrorBadArgumentParameter is
// returned:
//   - c.Command is larger than a 4-bit integer (0xf)
//   - c.StringLength does not indicate the actual length of c.String
//   - c.StringLength is greater than 1024