Middle
Mis juhtub, kui käivitate goroutine ja kutsute seal panic? Kas goroutine peatub või kogu rakendus?
sobes.tech AI
Vastus AI-lt
If a panic occurs in a goroutine, that specific goroutine will stop, but the entire application as a whole will not automatically shut down. A panic in a goroutine does not cause all goroutines or the program to stop unless it reaches the root level and remains unhandled.
To prevent an abrupt shutdown of the application, you can use recover within the same goroutine or in the calling function to catch and handle the panic.
Example:
func() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Panic caught in goroutine:", r)
}
}()
panic("something went wrong")
}()
In this example, the panic will not lead to the shutdown of the entire application, but will be handled inside the goroutine.