Escape Analysis is a process performed by the compiler where it decides whether or not an object escapes to the heap.
Objects escape to the heap if:
- A function returns a pointer
- A pointer is stored on an object that’s on the heap
- Passing a pointer to another function
- Using closures
There are multiple reasons an object is escaped to the heap, the most common one being if a function returning a pointer or storing a pointer to an object on the heap.
Use go build -gcflags=-m=3 [package]
to see decisions made by the complier escape analysis.