Manipulating Return Values with Defer
In Go, the defer statement can be used to execute a function after the surrounding function returns. This mechanism allows you to handle cleanup tasks or perform actions even if an error occurs. However, there is a limitation when attempting to return an error value from a function that panics and is enclosed in a defer statement.
Consider the following code snippet:
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 } }() panic("Report format not recognized.") // Remaining function code... }
The intention is to return an error if the getReport function panics. However, this approach doesn't work because defer functions cannot change the number of return values in the surrounding function. Instead, they can only modify the values of the existing return parameters.
To address this issue, the defer function should modify the err return parameter instead of trying to return a new error value:
defer func() { if r := recover(); r != nil { fmt.Println("Recovered in f", r) // Find out the exact error type and set err switch x := r.(type) { case string: err = errors.New(x) case error: err = x default: err = errors.New("Unknown panic") } // Invalidate rep rep = nil } }()
With this modification, the defer function updates the err return parameter and sets rep to nil to indicate an error condition. This allows the surrounding function to return the modified err value.
The above is the detailed content of Can Defer Statements Be Used to Manipulate Return Values in Panicked Functions?. For more information, please follow other related articles on the PHP Chinese website!