Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/website: #50344

Closed
HesamKorki opened this issue Dec 25, 2021 · 1 comment
Closed

x/website: #50344

HesamKorki opened this issue Dec 25, 2021 · 1 comment

Comments

@HesamKorki
Copy link

What is the URL of the page with the issue?

Defer, Panic, and Recover

What is your user agent?

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Screenshot

image

What did you do?

I was reading this great blog post, and it proposed that using "defer" in this situation would solve the problem of unclosed files. However, in the code snippet provided in the blog, this problem prevails as the defer statement came after the error handling. I was curious that maybe I'm wrong so I wrote a little dummy code to test if the defer would be effective even if it's located after a return statement. Turns out that it will not work as the blog post assumed.

package main

import "fmt"

func eh(){

        s:= "s"
        if s=="s"{
                fmt.Println("into if")
                return
        }
        defer fmt.Println("deffered end")

}
func main (){
        eh()
}

The output of this code when I run it is:

into if

It did not print out the "deferred end", but when I place the defer before the if block, it works as expected. So I thought I report this minor issue so that new comers like me would not get a wrong idea from the blog post.

@gopherbot gopherbot added this to the Unreleased milestone Dec 25, 2021
@seankhliao
Copy link
Member

if there is an error, there's nothing to close

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@golang golang locked and limited conversation to collaborators Dec 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants