Merge pull request #511 from qguv/master

[golang/en] added defer example
This commit is contained in:
Nami-Doc 2014-02-02 12:31:35 -08:00
commit 0d0375c73c

View File

@ -7,6 +7,7 @@ contributors:
- ["Sonia Keys", "https://github.com/soniakeys"] - ["Sonia Keys", "https://github.com/soniakeys"]
- ["Christopher Bess", "https://github.com/cbess"] - ["Christopher Bess", "https://github.com/cbess"]
- ["Jesse Johnson", "https://github.com/holocronweaver"] - ["Jesse Johnson", "https://github.com/holocronweaver"]
- ["Quint Guvernator", "https://github.com/qguv"]
--- ---
Go was created out of the need to get work done. It's not the latest trend Go was created out of the need to get work done. It's not the latest trend
@ -180,9 +181,21 @@ func learnFlowControl() {
goto love goto love
love: love:
learnDefer() // A quick detour to an important keyword.
learnInterfaces() // Good stuff coming up! learnInterfaces() // Good stuff coming up!
} }
func learnDefer() (ok bool) {
// deferred statements are executed just before the function returns.
defer fmt.Println("deferred statements execute in reverse (LIFO) order.")
defer fmt.Println("\nThis line is being printed first because")
// defer is commonly used to close a file, so the function closing the file
// stays close to the function opening the file
return true
}
// Define Stringer as an interface type with one method, String. // Define Stringer as an interface type with one method, String.
type Stringer interface { type Stringer interface {
String() string String() string