code is as follows:
package main
import (
"fmt"
"io"
"os"
)
func main() {
f := openFile()
useFile(f)
useFile2(f)
}
func openFile() *os.File {
//hello.txt world
f, _ := os.Open("hello.txt")
return f
}
func useFile(f *os.File) {
fmt.Println("f")
io.Copy(os.Stdout, f)
fmt.Println()
}
func useFile2(f *os.File) {
fmt.Println("f")
io.Copy(os.Stdout, f)
fmt.Println()
}
output:
using ffor the second time
world
for the first time using f
the second time I used f (* os.File), f was already closed, but I didn"t actively close it.
questions are as follows:
when will open files ( os.File) be closed if they are not actively closed? *
if I turn it off automatically after using it once, do I still need to turn it off actively?
ask the bosses to answer my questions