Skip to content

Instantly share code, notes, and snippets.

@crazyoptimist
Created July 31, 2024 20:05
Show Gist options
  • Save crazyoptimist/6ae6ecc09b227b1092d31fea5e29c398 to your computer and use it in GitHub Desktop.
Save crazyoptimist/6ae6ecc09b227b1092d31fea5e29c398 to your computer and use it in GitHub Desktop.
Guide on using recover()
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