Open pratham2003 opened 5 years ago
Take the example
package main
import "fmt"
func main() {
i :=1
defer fmt.Println(i+1)
i = 5
fmt.Println(i)
}
Output
5
2
The deferred call's arguments are evaluated immediately, but the function call is not executed until the surrounding function.
If the arguments were not evaluated immediately then output would be
5
6
Context: https://tour.golang.org/flowcontrol/12
I'm new to Go. Can someone suggest a better (easier to understand) example to explain the following statement? The deferred call's arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.
I tried something to explain the above but I feel it's too complicated to be introduced so early in the tour.