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:

  1. A function returns a pointer
  2. A pointer is stored on an object that’s on the heap
  3. Passing a pointer to another function
  4. 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.