mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2025-09-25 00:21:28 +02:00
24 lines
731 B
Markdown
24 lines
731 B
Markdown
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:
|
|
|
|
```go
|
|
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
|
|
}
|
|
``` |