如何将二进制文件读入结构体和反射 - Go 语言
我正在尝试编写一个程序,将二进制文件读取到Golang的一个结构中,方法是使用二进制软件包读取二进制文件以填充包含数组的结构,我使用数组而不是切片,因为我想指定字段长度,这似乎可以正常工作,但是当我尝试使用反射打印我们的字段值时,我会遇到此错误的
恐慌:反射:call of Reflect.value.value.bytes上的数组值,
代码
package main
import (
"encoding/binary"
"fmt"
"log"
"os"
"reflect"
)
type SomeStruct struct {
Field1 [4]byte
Field2 [2]byte
Field3 [1]byte
}
func main() {
f, err := os.Open("/Users/user/Downloads/file.bin")
if err != nil {
log.Fatalln(err)
}
defer f.Close()
s := SomeStruct{}
err = binary.Read(f, binary.LittleEndian, &s)
numOfFields := reflect.TypeOf(s).NumField()
ps := reflect.ValueOf(&s).Elem()
for i := 0; i < numOfFields; i++ {
value := ps.Field(i).Bytes()
for j := 0; j < len(value); j++ {
fmt.Print(value[j])
}
}
}
以下是当i时的 将代码更改为它
package main
import (
"encoding/binary"
"fmt"
"log"
"os"
"reflect"
)
type SomeStruct struct {
Field1 [4]byte
Field2 [2]byte
Field3 [1]byte
}
func main() {
f, err := os.Open("/Users/user/Downloads/file.bin")
if err != nil {
log.Fatalln(err)
}
defer f.Close()
s := SomeStruct{}
err = binary.Read(f, binary.LittleEndian, &s)
numOfFields := reflect.TypeOf(s).NumField()
ps := reflect.ValueOf(&s).Elem()
for i := 0; i < numOfFields; i++ {
value := ps.Field(i)
fmt.Print(value)
}
}
以其ASCII表示形式打印数组,我需要打印ASCII的炭化表示,并且当我感到恐慌
思想时?
I am trying to write a program to read a binary file into a struct in golang, the approach is to use the binary package to read a binary file to populate a struct that contains arrays, I am using arrays and not slices because I want to specify the field length, this seems to work fine but when I try to use reflection to print our the values of the fields I am getting this error
panic: reflect: call of reflect.Value.Bytes on array Value
Here is the code
package main
import (
"encoding/binary"
"fmt"
"log"
"os"
"reflect"
)
type SomeStruct struct {
Field1 [4]byte
Field2 [2]byte
Field3 [1]byte
}
func main() {
f, err := os.Open("/Users/user/Downloads/file.bin")
if err != nil {
log.Fatalln(err)
}
defer f.Close()
s := SomeStruct{}
err = binary.Read(f, binary.LittleEndian, &s)
numOfFields := reflect.TypeOf(s).NumField()
ps := reflect.ValueOf(&s).Elem()
for i := 0; i < numOfFields; i++ {
value := ps.Field(i).Bytes()
for j := 0; j < len(value); j++ {
fmt.Print(value[j])
}
}
}
when I change the code to this
package main
import (
"encoding/binary"
"fmt"
"log"
"os"
"reflect"
)
type SomeStruct struct {
Field1 [4]byte
Field2 [2]byte
Field3 [1]byte
}
func main() {
f, err := os.Open("/Users/user/Downloads/file.bin")
if err != nil {
log.Fatalln(err)
}
defer f.Close()
s := SomeStruct{}
err = binary.Read(f, binary.LittleEndian, &s)
numOfFields := reflect.TypeOf(s).NumField()
ps := reflect.ValueOf(&s).Elem()
for i := 0; i < numOfFields; i++ {
value := ps.Field(i)
fmt.Print(value)
}
}
it prints the arrays with their ascii representation, I need to print the char representation of the ascii and that when I get the panic
thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
bytes 文档说:
slice 获得一个字节的数组:
您也可以通过数组:
The Bytes documentation says:
Slice the array to get a slice of bytes:
You can also iterate through the array: