Home > Backend Development > Golang > 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
Release: 2024-11-14 12:10:02
Original
284 people have browsed it

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
    ...
}
Copy after login

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
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template