Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 使用struct读取二进制文件_File_Go_Struct_Binary - Fatal编程技术网

File 使用struct读取二进制文件

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)

我想用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)
}
但如果我优化了。阅读部分

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
}
谢谢:)现在我知道了(未)导出的消息:)