Golang closure problem

there are functions as follows
package main

import "fmt"

func intSeq () func () int {

i := 0
return func() int {
    i += 1
    return i
}

}
func main () {

nextInt := intSeq()
fmt.Println(nextInt())
fmt.Println(nextInt())
fmt.Println(nextInt())

newInts := intSeq()
fmt.Println(newInts())

}

in intSeq (), is the I in the anonymous function the same as the I in the external environment? Or is the external I in the stack and the I of the anonymous function in the heap?

Mar.30,2021

you can do a memory escape analysis and perform

go run -gcflags '-m -l' demo.go

you can see that the output is as follows:


-sharp command-line-arguments
./demo1.go:7:9: func literal escapes to heap
./demo1.go:7:9: func literal escapes to heap
./demo1.go:8:3: &i escapes to heap
./demo1.go:6:2: moved to heap: i
./demo1.go:15:21: nextInt() escapes to heap
./demo1.go:16:21: nextInt() escapes to heap
./demo1.go:17:21: nextInt() escapes to heap
./demo1.go:20:21: newInts() escapes to heap
./demo1.go:15:13: main ... argument does not escape
./demo1.go:16:13: main ... argument does not escape
./demo1.go:17:13: main ... argument does not escape
./demo1.go:20:13: main ... argument does not escape
1
2
3
1

you can see that both the iPermine I and nextInt functions escape from the stack space to the heap. & I is the I in the nextInt function.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b38665-2b962.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b38665-2b962.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?