如何将二进制文件读入结构体并使用反射 – Go语言

huangapple go评论88阅读模式
英文:

How to read a binary file into a struct and reflection - Go Language

问题

我正在尝试编写一个程序,将二进制文件读入到Golang中的结构体中。我的方法是使用binary包读取二进制文件,并将其填充到包含数组的结构体中。我使用数组而不是切片,因为我想指定字段的长度。这似乎工作得很好,但是当我尝试使用反射打印字段的值时,出现了以下错误:

panic: reflect: call of reflect.Value.Bytes on array Value

以下是代码:

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])
		}
	}
}

当我将代码更改为以下内容时:

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的字符表示形式,这就是我遇到panic的原因。

你有什么想法吗?

英文:

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 (
	&quot;encoding/binary&quot;
	&quot;fmt&quot;
	&quot;log&quot;
	&quot;os&quot;
	&quot;reflect&quot;
)

type SomeStruct struct {
	Field1                     [4]byte
	Field2                  [2]byte
	Field3                [1]byte

}

func main() {
	f, err := os.Open(&quot;/Users/user/Downloads/file.bin&quot;)
	if err != nil {
		log.Fatalln(err)
	}
	defer f.Close()

	s := SomeStruct{}
	err = binary.Read(f, binary.LittleEndian, &amp;s)

	numOfFields := reflect.TypeOf(s).NumField()
	ps := reflect.ValueOf(&amp;s).Elem()

	for i := 0; i &lt; numOfFields; i++ {
		value := ps.Field(i).Bytes()
		for j := 0; j &lt; len(value); j++ {
			fmt.Print(value[j])
		}
	}
}

when I change the code to this

package main

import (
	&quot;encoding/binary&quot;
	&quot;fmt&quot;
	&quot;log&quot;
	&quot;os&quot;
	&quot;reflect&quot;
)

type SomeStruct struct {
	Field1 [4]byte
	Field2 [2]byte
	Field3 [1]byte
}

func main() {
	f, err := os.Open(&quot;/Users/user/Downloads/file.bin&quot;)
	if err != nil {
		log.Fatalln(err)
	}
	defer f.Close()

	s := SomeStruct{}
	err = binary.Read(f, binary.LittleEndian, &amp;s)

	numOfFields := reflect.TypeOf(s).NumField()
	ps := reflect.ValueOf(&amp;s).Elem()

	for i := 0; i &lt; 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?

答案1

得分: 2

Bytes 文档中提到:

Bytes 返回 v 的底层值。如果 v 的底层值不是一个字节切片,则会引发 panic。

将数组切片化以获取字节切片:

field := ps.Field(i)
value := field.Slice(0, field.Len()).Bytes()
for j := 0; j < len(value); j++ {
    fmt.Print(value[j])
}

你也可以通过迭代数组来实现:

value := ps.Field(i)
for j := 0; j < value.Len(); j++ {
    fmt.Print(byte(value.Index(j).Uint()))
}
英文:

The Bytes documentation says:

> Bytes returns v's underlying value. It panics if v's underlying value is not a slice of bytes.

Slice the array to get a slice of bytes:

field := ps.Field(i)
value := field.Slice(0, field.Len()).Bytes()
for j := 0; j &lt; len(value); j++ {
    fmt.Print(value[j])
}

You can also iterate through the array:

value := ps.Field(i)
for j := 0; j &lt; value.Len(); j++ {
	fmt.Print(byte(value.Index(j).Uint()))
}

huangapple
  • 本文由 发表于 2022年3月30日 01:39:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/71666528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定