Last active
August 29, 2015 14:24
-
-
Save tdshipley/231bd1b3c696bd3357f3 to your computer and use it in GitHub Desktop.
Defer in Go example
This file contains 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
// 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