1
0
mirror of https://github.com/kamranahmedse/developer-roadmap.git synced 2025-09-25 00:21:28 +02:00
Files
developer-roadmap/src/data/question-groups/golang/content/error-handling-comparison.md
2025-04-22 12:44:42 +01:00

731 B

Go uses explicit error handling by returning an error value alongside other results.

This contrasts with the exception handling mechanisms found in other languages, leading to clearer code documentation and predictable error management paths.

Here's a quick example of what all of this looks like:

package main

import (
    "errors"
    "fmt"
)

// Divide divides two numbers and returns an error if division by zero is attempted.
func Divide(a, b float64) (float64, error) {
    if b == 0 {
        // Explicitly return an error value when b is zero.
        return 0, errors.New("division by zero is not allowed")
    }
    // Return the result and nil for error if division is successful.
    return a / b, nil
}