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
", name, h)
return h(name)
}
}
func main() {
var h Handler
h = func(name string) error {
fmt.Printf("********************************
")
fmt.Printf("before func h=%x
", h)
h = mh(h)
fmt.Printf("after func h=%x
", h)
return h(name)
}
fmt.Printf("main h=%x
", 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".