In many languages, local variables are located in call stack
In JavaScript/Python, only closure variables are located in heap, because they must live beyond the function calls, they are created.
In GO, some GO types(like slice type []int
) do reference other parts of memory, like JavaScript/Python.
In GO, not all types of variables hold references, like Javascript/Python.
For example,
1) [3]int
type variable b
directly stores an array of int
's, like C, except that C allows to get access of each array element location using C syntax &b[index]
, for more control
2) int
type variable c
directly stores an int
value, like C, except that, C gives more control by providing syntax(&c
) to get location access.
In GO, my understanding is, for local variables to be on heap/stack depends on applying compiler's escape analysis in example code(below),
func foo() []int {
// the array lives beyond the call to foo in which it is created
var a [5]int
return a[:] // range operator
}
that tells the compiler that variable a
lives beyond its scope, so allocate in heap, but not stack.
Question:
Does the variable a
get allocated in heap?