if err not equal nil ,defer not push stack yet,so it wont execute.
As below it will execute 

func main() {

   src, err := os.Open("text.txt")

   defer src.Close()

if err != nil {
return
}

}




On Friday, January 4, 2019 at 2:09:51 PM UTC+8, 伊藤和也 wrote:
>
> In the first example, "ok" was not printed.
>
> func main() {
>    if(true) {
>       return 
>    }
>    defer fmt.Println("ok")
> }
>
> So in the second example, is the Close function executed after an error 
> occurs and returned. 
>
> func main() {
>    src, err := os.Open("text.txt")
>    if err != nil {
>       return
>    }
>    defer src.Close()
> }
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to