Created
July 31, 2024 20:05
-
-
Save crazyoptimist/6ae6ecc09b227b1092d31fea5e29c398 to your computer and use it in GitHub Desktop.
Guide on using recover()
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" | |
) | |
var index int = 0 | |
func main() { | |
for range time.Tick(time.Second) { | |
work() | |
} | |
} | |
func work() { | |
defer func() { | |
if r := recover(); r != nil { | |
fmt.Printf("Recovered: %v\n", r) | |
} | |
}() | |
index++ | |
fmt.Println("Current index: ", index) | |
if index%3 == 0 { | |
panic(fmt.Sprintf("Panic occurred at index %d", index)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment