【发布时间】:2019-03-12 19:25:05
【问题描述】:
我想一次读取和处理文件名给定的文件中的 1024 个字节。我不明白如何正确构造外循环,尤其是为了适应缓冲区将包含少于 1024 个字节的最终步幅
我尝试过的:
fs, _ := os.Open(filename)
defer fs.Close()
n := 1024 // 1kb
buff := make([]byte, n)
for {
buff = make([]byte, n) // is this initialized correctly?
n1, err := fs.Read(buff)
if err != nil {
if err == io.EOF {
break
}
fmt.Println(err)
break
}
fmt.Println("read n1 bytes...", n1)
fmt.Println(buff)
}
我看过以下资源:
- Reading specific number of bytes from a buffered reader in golang
- Read arbitrary amount of bytes into buffer Golang
- How to read a binary file in Go
- Working with raw bytes from a network in go
- https://gobyexample.com/reading-files
- Writing into fixed size Buffers in Golang with offsets
- https://rwinslow.com/posts/use-flatbuffers-in-golang/
- reading file line by line in go
- How do I read in a large flat file in Golang
- https://golang.org/pkg/io/#ReadFull
【问题讨论】:
标签: go