If I have a program like this, it fails as expected (on Windows):

~~~
package main

import (
   "fmt"
   "os"
)

func main() {
   old := "go1.16.5.src.tar.gz"
   os.Open(old)
   err := os.Rename(old, "new.tar.gz")
   // The process cannot access the file because it is being used by 
another process.
   fmt.Println(err)
}
~~~

However this program succeeds:

~~~
package main

import (
   "compress/gzip"
   "os"
)

func main() {
   old := "go1.16.5.src.tar.gz"
   f, err := os.Open(old)
   if err != nil {
      panic(err)
   }
   gzip.NewReader(f)
   f.Close()
   os.Rename(old, "new.tar.gz")
}
~~~

even though I did not close the gzip reader. My question is: what is the 
point
of closing the gzip reader? What "bad" can happen if you dont do it?

https://golang.org/pkg/compress/gzip#Reader.Close

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/28eb98d4-c531-413d-b7f1-790b2600309fn%40googlegroups.com.

Reply via email to