Defer evaluates the arguments to the deferred func at the point  of the 
defer statement.
On Saturday, August 26, 2023 at 10:58:06 AM UTC-4 Aln Kapa wrote:

> Hi All ! 
> Need some help, what am I doing wrong?
>
> https://go.dev/play/p/bBlA-i1CxNO
>
> // You can edit this code!
> // Click here and start typing.
> package main
>
> import (
> "errors"
> "fmt"
> )
>
> type process struct {
> }
>
> func (p *process) close(err error) {
> if err != nil {
> fmt.Println("error")
> } else {
> fmt.Println("no error")
> }
> }
> func Handle(b bool) {
> p := process{}
> var err error
> defer func() {
> p.close(err)
> }()
> if b {
> err = errors.New("err")
> }
> }
>
> func HandleWTF(b bool) {
> p := process{}
> var err error
> defer p.close(err)
> if b {
> err = errors.New("err")
> }
> }
>
> func main() {
> Handle(true)    // error
> Handle(false)   // no error
> HandleWTF(true) // no error ?????????
> }
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/b72045bb-8a6f-4835-8f1a-3241e9b1dbf9n%40googlegroups.com.

Reply via email to