[go-nuts] Re: wtf

2023-08-27 Thread Jonathan
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

Re: [go-nuts] Re: wtf

2023-08-26 Thread Aln Kapa
I understand, thanks. On Sat, Aug 26, 2023, 18:47 Brian Candler wrote: > In both cases, the argument inside parenthesis is evaluated at the time > "defer" is executed - as indeed is the value of "p". > > In the first case, you wrote "defer p.close(err)". Since err is nil at > this point, at the

Re: [go-nuts] Re: wtf

2023-08-26 Thread Brian Candler
In both cases, the argument inside parenthesis is evaluated at the time "defer" is executed - as indeed is the value of "p". In the first case, you wrote "defer p.close(err)". Since err is nil at this point, at the end of the function body, p.close(nil) is called. In the second case, you wrote

Re: [go-nuts] Re: wtf

2023-08-26 Thread Aln Kapa
Well here is the code that works as I need, what is wrong with the previous one ? https://go.dev/play/p/ZW-GmEP5uqu package main import ( "fmt" ) type process struct { } func (p *process) close(err any) { v, _ := err.(*int) if *v == 1 { fmt.Println("error") } else { fmt.Println("no error") } }

[go-nuts] Re: wtf

2023-08-26 Thread Brian Candler
Any arguments to defer functions are evaluated at the time that the defer is executed. In HandleWTF, defer p.close(err) is called immediately after err is declared with value nil, so nil is what is used. >From the specification : "Each time a "defer" s