I am trying to create a temporary gzip file and write to the file. The problem is that I am not understanding what is going on with ReadAll. I expected ReadAll to return the bytes written to the file... however there are none. Yet the File.Stat command shows that there is indeed data.
filename := "test"
file, err := ioutil.TempFile("", filename)
if err != nil {
fmt.Println(err)
}
defer func() {
if err := os.Remove(file.Name()); err != nil {
fmt.Println(err)
}
}()
w := gzip.NewWriter(file)
_, err = w.Write([]byte("hell0"))
if err != nil {
fmt.Println(err)
}
fileInfo, err := file.Stat()
if err != nil {
fmt.Println(err)
}
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println(err)
}
if err := w.Close(); err != nil {
fmt.Println(err)
}
fmt.Println("SIZE1:", fileInfo.Size())
fmt.Println("SIZE2:", len(fileBytes))
Here is a playground link https://play.golang.org/p/zX8TSCAbRL
Why are there no returned bytes? How do I get the returned bytes?