从接收到的字节缓冲区服务器,我想复制结构。
缓冲区的格式是固定大小的字节,如下所示。
00000000 83 27 48 12 6c 00 00 00 01 02 00 00 01 01 00 02 |.'H.l...........|
00000010 10 01 d2 02 96 49 00 00 00 00 87 d6 12 00 00 00 |.....I..........|
00000020 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000030 00 02 01 02 3c 01 01 00 00 00 01 01 01 01 18 10 |....<...........|
00000040 2c 01 90 01 01 6c 07 03 c8 02 01 02 03 9c 0a 0b |,....l..........|
00000050 0c 00 00 00 01 01 00 00 00 00 00 00 00 01 01 01 |................|
00000060 01 01 01 01 01 01 01 01 01 00 01 01 01 00 00 00 |................|
我的结构在下面。
type HeaderT struct {
magicValue [8]byte
bodyLength [4]byte
bodyVersion [1]byte
...
}
我的实现在下面。
func onMessageReceived(client MQTT.Client, message MQTT.Message) {
payload := message.Payload()
fmt.Printf("Received message on topic: %s\nMessage: \n%s\n", message.Topic(), hex.Dump(payload))
header := HeaderT {}
err := binary.Read(bytes.NewBuffer(payload[:]), binary.LittleEndian, &header) // <-- error occurred at this line
...
}
我的代码如下所示引起恐慌。
守着星空守着你
相关分类