Skip to content

Instantly share code, notes, and snippets.

@tdshipley
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdshipley/231bd1b3c696bd3357f3 to your computer and use it in GitHub Desktop.
Save tdshipley/231bd1b3c696bd3357f3 to your computer and use it in GitHub Desktop.
Defer in Go example
// Taken from https://golang.org/doc/effective_go.html#defer
func Contents(filename string) (string, error) {
f, err := os.Open(filename)
if err != nil {
return "", err
}
defer f.Close() // f.Close will run when we're finished.
var result []byte
return string(result), nil // f closed here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment