Skip to content

Instantly share code, notes, and snippets.

@ozline
Created October 17, 2023 05:08
Show Gist options
  • Save ozline/da4211ab04caddaca691d200c9c1e56c to your computer and use it in GitHub Desktop.
Save ozline/da4211ab04caddaca691d200c9c1e56c to your computer and use it in GitHub Desktop.
test for defer
func main() {
fmt.Println(test1())
fmt.Println(test2())
fmt.Println(test3())
fmt.Println(test4())
return
}
func test1() (v int) {
defer fmt.Println(v)
return v
}
func test2() (v int) {
defer func() {
fmt.Println(v)
}()
return 3
}
func test3() (v int) {
defer fmt.Println(v)
v = 3
return 4
}
func test4() (v int) {
defer func(n int) {
fmt.Println(n)
}(v)
return 5
}
@ozline
Copy link
Author

ozline commented Oct 17, 2023

answer is here:

0
0
3
3
0
4
0
5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment