Sobes.tech
Junior — Middle

What error handling methods are you familiar with in Go?

sobes.tech AI

Answer from AI

In Go, error handling is usually done through the returned error value. Functions return two values: the result and the error. If the error is not nil, it needs to be handled. This approach allows explicit error control.

Example:

func divide(a, b float64) (float64, error) {
    if b == 0 {
        return 0, fmt.Errorf("division by zero")
    }
    return a / b, nil
}

result, err := divide(10, 0)
if err != nil {
    fmt.Println("Error:", err)
} else {
    fmt.Println("Result:", result)
}

Go also has panics and recover, but they are used for critical errors, not for regular error handling.

Sometimes custom error types are used for more detailed information.

What error handling methods are you familiar with in… - sobes.tech