1

I don't really understand how this code works, specifically e.(Exit), could someone explain?

type Exit struct{ Code int }
func handleExit() {
    if e := recover(); e != nil {
        if exit, ok := e.(Exit); ok == true {
            os.Exit(exit.Code)
        }
        panic(e)
     }
}

func main() {
    defer handleExit()
    panic(Exit{1})
}
2
  • Basically it tests if the interface value passed to panic() holds a value of type Exit, and if so, it extracts the exit code from it and passes it to os.Exit(), if not, it re-panics. Commented Nov 20, 2015 at 6:58
  • Yeah, I figured the flow was similar. I was really looking for what that operation is called. (TypeAssertion) Commented Nov 20, 2015 at 7:00

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.