Instantly share code, notes, and snippets.

Embed
What would you like to do?
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