Skip to content
This repository has been archived by the owner on Nov 22, 2018. It is now read-only.

Commit

Permalink
go-tour: add defer slides
Browse files Browse the repository at this point in the history
LGTM=adg
R=adg, campoy
CC=golang-codereviews
https://codereview.appspot.com/110520043

Committer: Andrew Gerrand <[email protected]>
  • Loading branch information
campoy committed Jul 23, 2014
1 parent cf43b78 commit d8e3192
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
20 changes: 20 additions & 0 deletions content/flowcontrol.article
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,26 @@ This construct can be a clean way to write long if-then-else chains.

.play prog/tour/switch-with-no-condition.go

* Defer

A defer statement defers the execution of a function until the surrounding
function returns.

The deferred call's arguments are evaluated immediately, but the function call
is not executed until the surrounding function returns.

.play prog/tour/defer.go

* Stacking defers

Deferred function calls are pushed onto a stack. When a function returns, its
deferred calls are executed in last-in-first-out order.

To learn more about defer statements read this
[[http://blog.golang.org/defer-panic-and-recover][blog post]].

.play prog/tour/defer-multi.go

* Congratulations!

You finished this lesson!
Expand Down
13 changes: 13 additions & 0 deletions content/prog/tour/defer-multi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import "fmt"

func main() {
fmt.Println("counting")

for i := 0; i < 10; i++ {
defer fmt.Println(i)
}

fmt.Println("done")
}
9 changes: 9 additions & 0 deletions content/prog/tour/defer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import "fmt"

func main() {
defer fmt.Println("world")

fmt.Println("hello")
}

0 comments on commit d8e3192

Please sign in to comment.