我正在尝试从 []byte 变量中读取结构。当我尝试读取单个变量时,我可以使其工作,但是当尝试直接读取结构时,我收到以下错误: panic: reflect: reflect.Value.SetInt using value obtained using unexported field这是我尝试过的:package mainimport ( "bytes" "encoding/binary" "fmt")type foo struct { a int8 b int8}func main() { var data1 int8 var data2 foo b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40} buf := bytes.NewReader(b) // READ data1 (int8) fmt.Println("READ data1") err := binary.Read(buf, binary.LittleEndian, &data1) if err != nil { fmt.Println("binary.Read failed:", err) } fmt.Println(data1) // READ data2 (struct with two int8s) fmt.Println(data2) err = binary.Read(buf, binary.LittleEndian, &data2) if err != nil { fmt.Println("binary.Read failed:", err) } fmt.Print(data2)}这是完整的错误:READ data124{0 0}panic: reflect: reflect.Value.SetInt using value obtained using unexported fieldgoroutine 1 [running]:reflect.flag.mustBeAssignableSlow(0x1a3) /usr/local/go-faketime/src/reflect/value.go:257 +0x1b9reflect.flag.mustBeAssignable(...) /usr/local/go-faketime/src/reflect/value.go:247reflect.Value.SetInt(0x4a9760, 0xc00010000c, 0x1a3, 0x2d) /usr/local/go-faketime/src/reflect/value.go:1633 +0x3bencoding/binary.(*decoder).value(0xc00006ae50, 0x4a9760, 0xc00010000c, 0x1a3) /usr/local/go-faketime/src/encoding/binary/binary.go:574 +0x9a5encoding/binary.(*decoder).value(0xc00006ae50, 0x4b17e0, 0xc00010000c, 0x199) /usr/local/go-faketime/src/encoding/binary/binary.go:558 +0x3a8encoding/binary.Read(0x4e2460, 0xc00010c000, 0x4e2ea0, 0x58c588, 0x4a5d80, 0xc00010000c, 0x0, 0x0) /usr/local/go-faketime/src/encoding/binary/binary.go:259 +0x33amain.main() /tmp/sandbox052619529/prog.go:36 +0x358Program exited: status 2.可以在此处找到可重现的示例:https://play.golang.org/p/4UWy2OuDAsL
1 回答
萧十郎
TA贡献1815条经验 获得超13个赞
问题是您的foo
结构字段未导出(第一个字母未大写),因此反射包无法访问那些进行写入(它仍然可以读取未导出的字段)。
将您的结构更改为:
type foo struct { A int8 B int8 }
- 1 回答
- 0 关注
- 94 浏览
添加回答
举报
0/150
提交
取消