Why gomonkey.ApplyFunc mocked func in for loop share the same address?

56 views Asked by At

I use gomonkey.ApplyFunc mock a func in for loop, then call it. Why it returns the same result every time, and the mocked func in loop all points to a same address.

func Test2(t *testing.T) {
    for i := 0; i < 3; i++ {
        gomonkey.ApplyFunc(env.Loc, func() int {
            tmp := i + 1
            return tmp
        })
        t.Logf("%p, %v", env.Loc, env.Loc())
    }
}

output:
0x8b85c0, 1
0x8b85c0, 1
0x8b85c0, 1
0

There are 0 answers