Skip to content

Instantly share code, notes, and snippets.

@gkampitakis
Created February 18, 2024 14:14
Show Gist options
  • Save gkampitakis/5bd619df11c36d896d59046d8437a753 to your computer and use it in GitHub Desktop.
Save gkampitakis/5bd619df11c36d896d59046d8437a753 to your computer and use it in GitHub Desktop.
Memory leaks in Go - Deferring function calls
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