How to understand this recursive result

Viewed 135

I wrote a bug while coding, when I solved the problem, I was confused about the output of the code,the code show as below:

type (
    Handler func(name string) error
)

func mh(h Handler) Handler {
    return func(name string) error {
        fmt.Printf("return mh,name=%s,h=%x\n", name, h)
        return h(name)
    }
}

func main() {
    var h Handler
    h = func(name string) error {
        fmt.Printf("********************************\n")
        fmt.Printf("before func h=%x\n", h)
        h = mh(h)
        fmt.Printf("after func h=%x\n", h)
        return h(name)
    }
    fmt.Printf("main h=%x\n", h)
    h("main")
}

Running the code, the output is:

main h=486d40
********************************
before func h=486d40
after func h=486c00
return mh,name=main,h=486d40
********************************
before func h=486c00
after func h=486c00
return mh,name=main,h=486c00
return mh,name=main,h=486d40
********************************
before func h=486c00
after func h=486c00
return mh,name=main,h=486c00
return mh,name=main,h=486c00
return mh,name=main,h=486d40
.......

I don't understand the call stack.I thought the output should be loop "mh".

2 Answers

The culprit is this assignment:

  h = mh(h) 

It replaces the binding of h in main from the anonymous function that prints out before/after to whatever mh returns.

If you replace

    h = mh(h)    
    fmt.Printf("after func h=%x\n", h)    
    return h(name)

With

    return mh(h)(name)

You will get the mutual recursion you expect

The key thing to understand is that this line:

h = mh(h)

Does not call the h function. It calls the mh() function, which just returns a function value, but it does not call h() either. If the returned function value would be called, that would call h().

So the main() function stores a function value in h, then calls h().

This h() prints "before", then wraps h in another function, and stores the result in h, then prints "after". It's important to know that the wrapper function (the value returned by mh()) is a closure, and it stores the original value of h, so assigning the result to h does not affect the h inside the wrapped function.

So h ends by calling h which is now the wrapped function. The wrapped function starts by printing "return", then it calls the original, unwrapped h.

The original, unwrapped h again prints "before", then it wraps the current value of h (which is the wrapped function), stores it in h, then prints "after".

And then calls h, which is now a 2-times wrapped function. It starts by printing "return", then calls the saved h value, which is a 1-time wrapped function. The 1-time wrapped function starts by printing "return" (again), then proceeds with the original one, which prints "before", wraps h which will now be 3-times wrapped, stores it in h, then calls h (which is the 3-times wrapped function value)...

This logic continues, the function value stored in h will become wrapped more and more, and the wrapped function always has a saved value of the one-less times wrapped previous function.

As the "iteration" continues, the "wrapping depth" increases, therefore you'll see more and more "return" statements printed (as that's what the wrapping does).

Related