What would it return the second time it's called?
This raises a lot of questions, the answers to which are non-obvious
(to me at least). The semantics of sync.Once is very simple; I'd go so
far as to say it's beautiful. Why complicate it, when you can get the
behaviour you need with easy-to-read code?
Andrew
how could it return different values? surely the whole
point of "once" is that the function is called once only.
so there's only one value to return.
your function foo() will always return nil the second
time it's called.
the spec would require it to return an interface{}. it seems like
special pleading to me. better to use the function it calls to write
down the value with the type you want. then after any call to
once.Do(f), that value is available, safely.
-rob
Do you want a function that returns the same output for the same input
without having to redo the calculations? That's not what once.Do is
for.
have you tried it?
assuming oncePasswd remains the same, the argument to oncePasswd.Do
will be evaluated only once - i.e. the value of f and err will be
changed only once, on the first call to GetpwByUid. on subsequent
calls, even though a new closure is created, it will not be called.
yes, that's definitely better.
i said it was an easy change to make - i wasn't trying to imply
that i wanted it to happen.