Panic With String (ARCH5167)
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
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.
Incorrect
Section titled “Incorrect”func divide(a, b int) int { if b == 0 { panic("division by zero") } return a / b}Correct
Section titled “Correct”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.