Skip to content

Instantly share code, notes, and snippets.

@gkampitakis
Created February 18, 2024 14:03
Show Gist options
  • Save gkampitakis/1ea36c7b390867e7b6dc4b078e0ca6fc to your computer and use it in GitHub Desktop.
Save gkampitakis/1ea36c7b390867e7b6dc4b078e0ca6fc to your computer and use it in GitHub Desktop.
Memory leaks in Go - Long lived references
func readFileDetails(name string) []byte{
data, err := os.ReadFile(name)
if err != nil {
return err
}
return data[5:10]
}
// not holding reference to underlying memory anymore
func readFileDetails(name string) []byte{
data, err := os.ReadFile(name)
if err != nil {
return err
}
return bytes.Clone(data[5:10]))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment