from the book: The Body stream is closed to avoid leaking resources.
I would have thought that a modern language would automatically take care of such things.
I would have thought that a modern language would automatically take care of such things.
Go does provide a "defer" statement, which cause a function call to execute when the current function returns. This makes it easy to visually co-locate resource acquisition and release, as well as ensuring the release code runs regardless of how the function is exited.
It looks like this:
f := os.Open("someFile")
defer f.Close() // will run when current function exits
It's easy and works well (IMO).For this reason Java has try-with-resources, C# using, Haskell bracket and Go defer.
Note that you can attach a finalizer to a Go struct: https://golang.org/pkg/runtime/#SetFinalizer
The only thing modern about Go is that it's just a few years old, but it's based on programming language concepts that were mainstream in the late 90s.