There are two potential negative consequences I could imagine happening:
1. You don't get notified about checksum mismatches or the like. i.e. the
data you read might be corrupted and you might not realize it.
2. There might, theoretically, be goroutines spawned which get leaked if
you don't `Close` the `Reader`. I'd assume this is unlikely (I can't see a
decompressor really benefiting from spawning goroutines) but it's possible.

Either way, it's also just good hygiene to *always* call `Close` on an
`io.Closer`. If a library gives you a `Close` method, it usually expects
you to call it and there's really no point in not doing it.

On Mon, Jun 21, 2021 at 10:33 PM Steven Penny <srp...@gmail.com> wrote:

> 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
> <https://groups.google.com/d/msgid/golang-nuts/28eb98d4-c531-413d-b7f1-790b2600309fn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/CAEkBMfFj4vATVLQqBY5MJo%2BCk%2BqRkxdhF_3%2BQm5OA53KG4E7bw%40mail.gmail.com.

Reply via email to