package main
import "fmt"
func main(){
x:=1;
if x==1{
fmt.Print("Hello ");
return
}
defer fmt.Print("Bye");
}
Why the output of the program is not Hello Bye. As defer statement should be executed after returning it.Please Explain?
Top comments (4)
Program flow exits before even registering a defer call onto the stack.
Try putting defer statement before if condition.
This is likely the problem.
The compiler is ignoring anything after the return, if you move the defer above your return statement is should work as expected.
Thanks @blazingbits for your reply.
Thanks @pranj010 for reply.