A stack frame is the runtime memory interval created when Go enters a function. It holds that invocation’s local variables and remains accessible only while execution is inside the function. Each goroutine has its own growing, contiguous stack. section 12.5.1
Mistake: treating returned locals as ordinary stack data
Calling sumValue creates a frame containing x, y, and z alongside main’s frame containing a and b. When sumValue returns, its frame is no longer accessible, although its old bytes may remain temporarily. Returning &z therefore requires Go to preserve z outside the ordinary frame lifetime rather than leaving callers with a usable stack address. section 12.5.1
func main() {
a := 3
b := 2
c := sumValue(a, b) #1
println(c) #2
}
//go:noinline #3
func sumValue(x, y int) int {
z := x + y
return z
}
Trace main and sumValue as separate frames: main allocates a and b, sumValue allocates x, y, and z, and returning removes sumValue’s frame from reachable execution state. section 12.5.1


