package pnyx import ( "encoding/binary" "fmt" ) type Packet struct { Channel ChannelID Mode ModeID Command byte Data []byte } func(packet Packet) String() string { return fmt.Sprintf("{Channel: %x, Mode: %x, Data: %x}", packet.Channel, packet.Mode, packet.Data) } func(packet Packet) MarshalBinary() ([]byte, error) { p := binary.BigEndian.AppendUint32(nil, uint32(packet.Channel)) p = append(p, byte(packet.Mode)) p = append(p, byte(packet.Command)) return append(p, packet.Data...), nil } func ParsePacket(data []byte) (*Packet, error) { if len(data) < 6 { return nil, fmt.Errorf("Not enough bytes to parse Packet(%d/%d)", len(data), 6) } return &Packet{ Channel: ChannelID(binary.BigEndian.Uint32(data)), Mode: ModeID(data[4]), Command: data[5], Data: data[6:], }, nil }