Created
July 15, 2012 21:09
-
-
Save devrim/3118634 to your computer and use it in GitHub Desktop.
go defer func
This file contains hidden or 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() { | |
ret++ | |
}() | |
return 1 | |
} | |
func main() { | |
fmt.Println(f()) | |
} |
return doesn't actually return a value, it just ends the function's regular execution and sets the return values. those values can still be changed by a deferred function call, as your code demonstrates.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This prints 2.
Problem: if defer() runs after 1 is returned, changing ret var after return 1 is returned, main() should print 1..