Browse files

a defer demo

  • Loading branch information...
1 parent bc5c873 commit 9558a3c7c104f94f9bd8c088e99238547a505ed1 @kicool committed Jan 29, 2013
Showing with 39 additions and 0 deletions.
  1. +39 −0 defer/defer.go
View
39 defer/defer.go
@@ -0,0 +1,39 @@
+package main
+
+import (
+ "fmt"
+ "log"
+)
+
+func bye(s string) {
+ log.Println("bye:", s)
+}
+
+func foo(i int) {
+ log.Println("IN Foo ", i)
+ defer bye(fmt.Sprintf("Defer3 Loop%d", i))
+ log.Println("OUT Foo ", i)
+
+}
+
+func main() {
+ log.Println("Start")
+
+ {
+ log.Println("IN1")
+ defer bye("Defer1")
+ log.Println("OUT1")
+ }
+
+ for i := 0; i < 3; i++ {
+ log.Println("IN2 ", i)
+ defer bye(fmt.Sprintf("Defer2 Loop%d", i))
+ log.Println("OUT2 ", i)
+ }
+
+ for i := 0; i < 3; i++ {
+ foo(i)
+ }
+
+ log.Println("End")
+}

0 comments on commit 9558a3c

Please sign in to comment.