Created
November 28, 2012 21:45
-
-
Save fipar/4164832 to your computer and use it in GitHub Desktop.
Basic example of panic / recover use in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func mainLoop() { | |
fmt.Println("starting main loop, this will die after dividing by 0") | |
i := 0 | |
fmt.Println("%d",1/i) | |
} | |
func main() { | |
defer func() { | |
if err := recover(); err != nil { | |
time.Sleep(2 * time.Second) | |
main() | |
} | |
}() | |
mainLoop() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment