Go language function execution problems, beginners to consult

package main
import "fmt"
func main(){
    p1:=test()
    p2:=test()
    fmt.Println(p1(),p2(),p1())
}
func test() func ()int {
    t:=0
    return func() int {
        tPP
        return t
    }
}

the above is one of my exercises, but the return result is a little different from what I expected. The return result is: 1 br 2. Can someone give me some advice? I think the return value should be 1 minute 1.
my question:
1. When you finish executing p1:=test (), you can get the result:

    p1:=func()int{
        tPP
        return t
    }

when executing p1 (), t within the function belongs to the local variable of the function test (). Why can p1 () still be used?
2. The first execution of p1 (), and then the execution of p1 () why t will accumulate, in fact, this is essentially the same as the first question.

the above is what I don"t understand. I hope you can give me some advice. I am a novice in Go learning. The question may be a little simple. I hope you will understand.

Apr.05,2022

the answer should be 1, 1, 1, 2. The landlord said that 1pm 2pm 1 should be a clerical error.
first of all, the statement t: = 0 is executed only when the two lines p1:=test () p2:=test () are executed, followed by a call to p1 (), p2 (), p1 () is the return value function of test () . t does not return to zero.
secondly, p1 and p2 are two different function stacks, and t is the variable assigned within the function stack, so t in p1 and p2 are actually two T1 and T2 .


shouldn't the execution result be 1 / 12? How is it 1 / 2 / 1?


answer incorrectly, find out and then come back

Menu