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})
}
Jared Mackey
  • 3,998
  • 4
  • 31
  • 50
  • 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. – icza Nov 20 '15 at 06:58
  • Yeah, I figured the flow was similar. I was really looking for what that operation is called. (TypeAssertion) – Jared Mackey Nov 20 '15 at 07:00

0 Answers0