Skip to content

Instantly share code, notes, and snippets.

@gkampitakis
Created February 18, 2024 14:16
Show Gist options
  • Save gkampitakis/bb2c65d07af7602d26c252e2e6a40385 to your computer and use it in GitHub Desktop.
Save gkampitakis/bb2c65d07af7602d26c252e2e6a40385 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 {
err := process(file)
if err != nil {
return err
}
}
return nil
}
func process(name string) error {
f, err := os.Open(name)
if err != nil {
return err
}
defer f.Close()
// do something with the file
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment