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

Could you add an example for recover()? #46

Closed
LarryBattle opened this issue May 6, 2013 · 6 comments
Closed

Could you add an example for recover()? #46

LarryBattle opened this issue May 6, 2013 · 6 comments

Comments

@LarryBattle
Copy link

Here's an example I wrote up for recover(). It would be nice if you included this in your examples.

package main

import "fmt"

func gandalf( doTalkTo bool ){
    defer catch()
    if(doTalkTo){
        // Panic stops execution of the current function and stops unwinding the stack, calling an deferred functions along the way.
        panic("You shall not pass!")    
    }
}
// catch() must be called as a deferred 
func catch(){
    // recover() regains control over the program execution when panic() is called.
    // recover() returns is the argument passed from panic()
    if r := recover(); r != nil {
        fmt.Println("Error:", r)
    } else {
        fmt.Println("No problems occurred")
    }
}
func main() {
    gandalf(true)
    gandalf(false)
    //unwinds to the top of the stack and ends the program
    panic("Where am I going?");
}

// Output:
// Error: You shall not pass!
// No problems occurred
// panic: Where am I going?
// goroutine 1 [running]:
// main.main()

Demo: http://play.golang.org/p/xu9Jd1YI0T

More information here:
Effective Go - The Program Language

@mmcgrana
Copy link
Owner

Sorry I'm only responding to this issue now /:

I'm actually not planning to add any more sections to Go by Example. recover is a very reasonable idea but due to personal bandwidth limitations I need to not increase the scope of the site at this point. Sorry!

@kilaka
Copy link

kilaka commented Feb 9, 2021

Can we reopen this as the community have grown and we can/want to help :)

@mmcgrana
Copy link
Owner

Yes we're in a better position to consider this now!

@mmcgrana mmcgrana reopened this Feb 14, 2021
@eliben
Copy link
Collaborator

eliben commented Feb 15, 2021

An example for recover sounds like a good idea, but I'd personally prefer something simpler than the proposal in this issue.

@kilaka
Copy link

kilaka commented Feb 15, 2021

I've created a PR using a fork.
Have two problems which I hope you have the knowledge to fix them quickly:

  1. Some strange auto-generated chars, which changed from \x3D to \u003D, causing a lot of files to look as if they were changed, but actually were not.
  2. Misalignment between explanation and code.

#344

@eliben, @mmcgrana, can you see if it looks OK?
Feel free to change/comment.

@eliben eliben mentioned this issue Feb 15, 2021
@eliben eliben closed this as completed in 91c8cee Sep 1, 2021
@eliben
Copy link
Collaborator

eliben commented Sep 1, 2021

Thanks for discussion. I ended up adding a simplified example (see linked commit). Please open an issue/PR if you have questions or suggestions about the new example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants