Created
September 29, 2013 08:03
-
-
Save tenntenn/6750284 to your computer and use it in GitHub Desktop.
[Go言語] deferに関する実験 ref: http://qiita.com/tenntenn/items/9aebce917760f157c44e
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
package main | |
import "fmt" | |
func f() { | |
defer fmt.Println("1") | |
defer fmt.Println("2") | |
defer fmt.Println("3") | |
} | |
func main() { | |
f() | |
} |
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
package main | |
import "fmt" | |
func f() { | |
i := 0 | |
defer fmt.Printf("i is %d", i) | |
i = 100 | |
} | |
func main() { | |
f() | |
} |
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
package main | |
import "fmt" | |
func f() (ret int) { | |
defer func(n int) { | |
fmt.Println(n) | |
fmt.Println(ret) | |
}(ret) // retはゼロ値で初期化されている | |
return 100 | |
} | |
func main() { | |
fmt.Println(f()) | |
} |
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
package main | |
import "fmt" | |
type Hoge struct { | |
N int | |
} | |
func (h *Hoge) PrintN() { | |
fmt.Println(h.N) | |
} | |
func f() { | |
h := &Hoge{100} | |
m := h.PrintN | |
defer m() | |
h.N = 200 | |
} | |
func main() { | |
f() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment