Defer in if-statements
- Even if we put the defer call inside an if-statement, the deferred function will only execute at the end of the enclosing function.
package main
import (
"fmt"
)
func main() {
if true {
fmt.Println("Before")
defer fmt.Println("Middle")
fmt.Println("After")
}
fmt.Println("Outside")
}