Programming Languages
Go
Subjective
Oct 04, 2025
Explain error handling in Go.
Detailed Explanation
Error handling in Go:
• Errors are values, not exceptions
• Functions return error as last return value
• Check errors explicitly
• Use panic/recover for exceptional cases
• Custom error types possible
Example:
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
}
result, err := divide(10, 0)
if err != nil {
log.Fatal(err)
}
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts