ove*_*ded 4 binary struct file go
我正在尝试使用 golang 读取二进制文件,但有一个问题。
如果我这样读,一切都会好起来的
package main
import (
"encoding/binary"
"fmt"
"os"
)
type Header struct {
str1 int32
str2 [255]byte
str3 float64
}
func main() {
path := "test.BIN"
file, _ := os.Open(path)
defer file.Close()
thing := Header{}
binary.Read(file, binary.LittleEndian, &thing.str1)
binary.Read(file, binary.LittleEndian, &thing.str2)
binary.Read(file, binary.LittleEndian, &thing.str3)
fmt.Println(thing)
}
Run Code Online (Sandbox Code Playgroud)
但是如果我将 .Read-Section 优化为
binary.Read(file, binary.LittleEndian, &thing)
//binary.Read(file, binary.LittleEndian, &thing.str1)
//binary.Read(file, binary.LittleEndian, &thing.str2)
//binary.Read(file, binary.LittleEndian, &thing.str3)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
panic: reflect: reflect.Value.SetInt using value obtained using unexported field
Run Code Online (Sandbox Code Playgroud)
谁能告诉我为什么?
所有示例均使用“优化方式”
谢谢 :)
str1、str2、 和str3未导出。这意味着其他包看不到它们。要导出它们,请将第一个字母大写。
type Header struct {
Str1 int32
Str2 [255]byte
Str3 float64
}
Run Code Online (Sandbox Code Playgroud)