Created
February 18, 2024 14:14
-
-
Save gkampitakis/5bd619df11c36d896d59046d8437a753 to your computer and use it in GitHub Desktop.
Memory leaks in Go - Deferring function calls
This file contains 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 processManyFiles(files []string) error { | |
for _, file := range files { | |
f, err := os.Open(file) | |
if err != nil { | |
return err | |
} | |
defer f.Close() | |
process(f) | |
} | |
return nil | |
} | |
func process(f *os.File) { | |
// do something with the file | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment