By default, the following objects are completed:
os. File : the file closes automatically when the object collects garbage.
os. Process : Termination returns any resources associated with the process. On Unix, this is not an operation. On Windows, it closes the handle associated with the process.
On Windows, it seems that the net packet might automatically close the network connection.
The Go standard library does not set a finalizer for objects other than those mentioned above.
It seems that there is only one potential problem that can cause problems in real programs. When os.File completes, it will make an OS call to close the file descriptor. If os.File was created by calling the os.NewFile(fd int, name string) *File function, and the file descriptor is also used by another (other) os.File , then garbage collection, or one of the file objects, will make the other The file object is unusable. For instance:
package main import ( "fmt" "os" "runtime" ) func open() { os.NewFile(1, "stdout") } func main() { open() // Force finalization of unreachable objects _ = make([]byte, 1e7) runtime.GC() _, err := fmt.Println("some text") // Print something via os.Stdout if err != nil { fmt.Fprintln(os.Stderr, "could not print the text") } }
prints:
could not print the text
user811773
source share