Let us say that I have the following code:
func getConnection(fileName string) *os.File {
file, err := os.Open(fileName)
//Check for error
return file
}
I use this function to open a file and the function is called from another function that does some other activity.
My question is, now that I have opened the file, how do I close it. If I were to add defer file.Close()
inside getConnection()
, wouldn't it close the file before returning? Does it make sense to use defer inside the calling function?