Created
October 17, 2023 05:08
-
-
Save ozline/da4211ab04caddaca691d200c9c1e56c to your computer and use it in GitHub Desktop.
test for defer
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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
answer is here: