-
-
Save hxzhouh/5bb1d55259dcb4dab87b37beaef9bea2 to your computer and use it in GitHub Desktop.
close file by AddCleanup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func NewFileResource(filename string) (*os.File, error) { | |
| file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0666) | |
| if err != nil { | |
| return nil, err | |
| } | |
| runtime.AddCleanup(file, func(f *os.File) { | |
| fmt.Println("close f") | |
| _ = f.Close() | |
| }, file) | |
| return file, nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment