Home  >  Article  >  Backend Development  >  Why is `return nil, err` incorrect in a `defer` function used for error handling in Go?

Why is `return nil, err` incorrect in a `defer` function used for error handling in Go?

Linda Hamilton
Linda HamiltonOriginal
2024-11-14 12:10:02201browse

Why is `return nil, err` incorrect in a `defer` function used for error handling in Go?

Go: Handle Panics with Defer

Panic and defer in Go offer a mechanism to gracefully handle errors and clean up resources. However, using defer and panic for error handling requires clarification.

Consider the following code:

func getReport(filename string) (rep report, err error) {
    rep.data = make(map[string]float64)

    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Recovered in f", r)
            err, _ = r.(error)
            return nil, err // Code 1
        }
    }()
    panic("Report format not recognized.")
    // rest of the getReport function, which can try to out-of-bound-access a slice
    ...
}

The intention here is to return an error if the function panics. However, the defer function uses return nil, err (marked as Code 1) to return from the current function. This is not the correct approach.

In a defer function, you can modify the returned parameters, but you cannot return a new set of values. To fix this, replace Code 1 with:

rep = nil
err = errors.New(r.(string))
return

This correctly sets the error and invalidates the report.

Additionally, it's important to note that the r value returned by recover() is of type interface{}. In the code above, it is type asserted to an error. If the panic value is not an error, you should use a more appropriate type assertion or handle it as an unknown panic.

The above is the detailed content of Why is `return nil, err` incorrect in a `defer` function used for error handling in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn