Ir al contenido

Panic With String (ARCH5167)

Esta página aún no está disponible en tu idioma.

Key go.logging.panic-with-string · Severity info · Category logging

Panicking with a raw string forces callers into a broad recover that cannot distinguish the error type. Errors should be returned as typed error values so the caller can inspect them with errors.Is/errors.As.

func divide(a, b int) int {
if b == 0 {
panic("division by zero")
}
return a / b
}
var ErrDivideByZero = errors.New("divide by zero")
func divide(a, b int) (int, error) {
if b == 0 {
return 0, ErrDivideByZero
}
return a / b, nil
}

Return a typed error value instead of panicking with a string.