I'd note the following:
https://play.golang.org/p/U_lmt-go6M

Also, this can be simplified to the following:
https://play.golang.org/p/eFnM98yRLk

Such a puzzle should look something more like:
https://play.golang.org/p/8KD6INL9QP


On Sun, Nov 19, 2017 at 6:25 PM Carl Mastrangelo <carl.mastrang...@gmail.com>
wrote:

> Hi gophers,
>
> I was playing around with a puzzle trying to break the sync package and
> found something cool.   Can you think of a definition for f that causes
> once.Do to execute the argument more than once?
>
>
> package main
>
> import (
>     "sync"
> )
>
> func main() {
>     var once sync.Once
>     var f = // ...
>
>     once.Do(f)
> }
>
>
> HIGHLIGHT BELOW FOR ANSWER
>
>
> package main
>
> import (
>     "fmt"
>     "sync"
> )
>
> func main() {
>     var once sync.Once
>     var f func()
>     times := 9
>     f = func() {
>         if times == 0 {
>             return
>         }
>         times--
>         fmt.Println("Called")
>         oldonce := once
>         *&once = sync.Once{}
>         once.Do(f)
>         once = oldonce
>
>     }
>     once.Do(f)
> }
>
> HIGHLIGHT ABOVE FOR ANSWER
>
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to