Example code:
package main
import (
"errors"
"fmt"
)
func main() {
err := errors.New("error 1")
defer fmt.Println(err)
err = errors.New("error 2")
}
In this case, I want fmt.Println to print out error 2.
err is already defined when you set the defer so what you what you likely want to do is wrap it in a func like below. Hope this helps.
package main
import (
"errors"
"fmt"
)
func main() {
err := errors.New("error 1")
defer func() {
fmt.Println(err)
}()
err = errors.New("error 2")
}
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