Created
February 18, 2024 14:16
-
-
Save gkampitakis/bb2c65d07af7602d26c252e2e6a40385 to your computer and use it in GitHub Desktop.
Memory leaks in Go - Deferring function calls
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 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