dongwo6477 2018-11-14 05:45
浏览 78
已采纳

恐慌:SetUint使用使用未导出字段获得的值

From byte buffer received server, I want to copy struct.

The format of buffer is fixed size bytes as below.

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

My struct is below.

type HeaderT struct {
    magicValue [8]byte
    bodyLength [4]byte
    bodyVersion [1]byte
    ...
}

My implementation is at below.

func onMessageReceived(client MQTT.Client, message MQTT.Message) {
    payload := message.Payload()
    fmt.Printf("Received message on topic: %s
Message: 
%s
", message.Topic(), hex.Dump(payload))

    header := HeaderT {}
    err := binary.Read(bytes.NewBuffer(payload[:]), binary.LittleEndian, &header)  // <-- error occurred at this line
    ...
}

My code make panic as below.

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)

  • 写回答

1条回答 默认 最新

  • dpwdldgn43486 2018-11-14 08:25
    关注

    The issue is that none of HeaderT's fields are "public".

    Notice that all the fields start with a lowercase letter - that means the fields are unreachable to any code outside of your package.

    From the 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.

    Try Exporting them by capitalizing their names:

    type HeaderT struct {
        MagicValue [8]byte
        BodyLength [4]byte
        BodyVersion [1]byte
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。