File 使用struct读取二进制文件
我想用golang读取二进制文件,但有个问题 如果我这样读的话,一切都会好起来的File 使用struct读取二进制文件,file,go,struct,binary,File,Go,Struct,Binary,我想用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)
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)
}
但如果我优化了。阅读部分
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)
我得到以下错误:
panic: reflect: reflect.Value.SetInt using value obtained using unexported field
有人能告诉我为什么吗
所有示例都使用“优化方式”
谢谢:)
str1
、str2
和str3
未报告。这意味着其他软件包无法看到它们。要导出它们,请将第一个字母大写
type Header struct {
Str1 int32
Str2 [255]byte
Str3 float64
}
谢谢:)现在我知道了(未)导出的消息:)