I am learning how defer behaves in golang, and want to use it to handle error when the function returns.
Code is as below:
package main
import "fmt"
import "errors"
func main() {
a()
}
func a() {
var err error
defer func(){
if err != nil {
fmt.Printf("1st defer: %s\n", err)
} else {
fmt.Println("1st defer: defer not error")
}
}()
defer func(err error){
if err != nil {
fmt.Printf("2nd defer: %s\n", err)
} else {
fmt.Println("2nd defer: defer not error")
}
}(err)
err = errors.New("new error")
if err != nil {
return
}
}
The output:
2nd defer: defer not error
1st defer: new error
Doc says parameters are evaluated when the defer call is evaluated, which seems it should be consistent. Why 2 defer has different value for variable err
and thusly different output? I know it is related to 2nd function has err
as input parameter, but don't know why.
In Go language, defer statements delay the execution of the function or method or an anonymous method until the nearby functions returns. In other words, defer function or method call arguments evaluate instantly, but they don't execute until the nearby functions returns.
In Golang, the defer keyword is used to delay the execution of a function or a statement until the nearby function returns. In simple words, defer will move the execution of the statement to the very end inside a function.
A defer statement defers the execution of a function until the surrounding function returns. The deferred call's arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.
Panics happen after a deferred statement is executed. the program ends after the panic statement is run.
There is another similar situation there is in case of Defer Statement
and Defer Function
. Please have look at the example below
package main
import (
"fmt"
"time"
)
func main() {
start := time.Now()
time.Sleep(3*time.Second)
defer func() { fmt.Println("Defer Function Elapsed Time: ", time.Since(start)) }() //Defer Function
defer fmt.Println("Defer Statement Elapsed Time: ", time.Since(start)) //Defer Statement
time.Sleep(3*time.Second)
}
Output:
Defer Statement Elapsed Time: 3s
Defer Function Elapsed Time: 6s
Try above in go play
This is because of in the Deferred Statement
case the deferred call's arguments are evaluated immediately
refer doc
Another way is by using reference to original err variable
package main
import (
"errors"
"fmt"
)
func main() {
a()
}
func a() {
var err error
defer func() {
if err != nil {
fmt.Printf("1st defer: %s\n", err)
} else {
fmt.Println("1st defer: defer not error")
}
}()
defer func(err *error) {
if *err != nil {
fmt.Printf("2nd defer: %s\n", *err)
} else {
fmt.Println("2nd defer: defer not error")
}
}(&err)
err = errors.New("new error")
if err != nil {
return
}
}
And output is:
2nd defer: new error
1st defer: new error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With