arrays - panic : SetUint using value obtained using unexported field

标签 arrays go byte

从服务器接收的字节缓冲区中,我想复制结构。

缓冲区的格式是固定大小字节,如下所示。

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
    ...
}

我的代码如下所示。

panic: reflect: reflect.Value.SetUint using value obtained using unexported field

goroutine 38 [running]: reflect.flag.mustBeAssignable(0x1a8) /usr/local/go/src/reflect/value.go:231 +0x1ee reflect.Value.SetUint(0x12540e0, 0xc0001a2000, 0x1a8, 0x83) /usr/local/go/src/reflect/value.go:1551 +0x2f encoding/binary.(*decoder).value(0xc000148d88, 0x12540e0, 0xc0001a2000, 0x1a8) /usr/local/go/src/encoding/binary/binary.go:548 +0x7c6 encoding/binary.(*decoder).value(0xc000148d88, 0x125cfc0, 0xc0001a2000, 0x1b1) /usr/local/go/src/encoding/binary/binary.go:510 +0x104 encoding/binary.(*decoder).value(0xc000148d88, 0x129fa00, 0xc0001a2000, 0x199) /usr/local/go/src/encoding/binary/binary.go:523 +0x2c5 encoding/binary.Read(0x12fcf80, 0xc00018a150, 0x1300c60, 0x14d76d0, 0x1248040, 0xc0001a2000, 0x0, 0x0) /usr/local/go/src/encoding/binary/binary.go:248 +0x342 main.onMessageReceived(0x13012a0, 0xc000140000, 0x1300c00, 0xc000192000)

最佳答案

问题是 HeaderT 的字段都不是“公共(public)”的。

请注意,所有字段都以小写字母开头 - 这意味着包外部的任何代码都无法访问这些字段。

来自spec :

Exported identifiers

An identifier may be exported to permit access to it from another package. An identifier is exported if both:

  • the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
  • the identifier is declared in the package block or it is a field name or method name.

All other identifiers are not exported.

尝试通过大写名称来导出它们:

type HeaderT struct {
    MagicValue [8]byte
    BodyLength [4]byte
    BodyVersion [1]byte
    ...
}

关于arrays - panic : SetUint using value obtained using unexported field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53293862/

相关文章:

C++将数据读入结构数组

go - 观察循环内的无效值

go - 带有指针接收器的方法

Python 将字符串转换为字节

Java - 优化将值作为位写入字节缓冲区

C 字符串处理

Python - NumPy - 元组作为数组的元素

c++ - golang 中是否有一些方法像 C++ 中的 std::bind 那样操作函数?

c# - 如何将byte[]作为文档发送给客户端?

python - 如何使 1d 数组乘以 2d 数组得到 python 的 3d 数组