The error "file already closed" occurs because you are attempting to read from or write to a file after calling Close() on it. Ensure all read or write operations happen before the Close() call, and check that you are not accidentally closing the file in a loop or defer statement before you are finished with it.
f, err := os.Open("data.txt")
if err != nil {
log.Fatal(err)
}
defer f.Close()
// Perform all operations here
data, err := io.ReadAll(f)
if err != nil {
log.Fatal(err)
}
// File is automatically closed here by defer
_ = data