Skip to content

Instantly share code, notes, and snippets.

@tdshipley tdshipley/deferInGo.go
Last active Aug 29, 2015

What would you like to do?
Defer in Go example
// Taken from
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
You can’t perform that action at this time.